Node.js
TypeORM
routing-controllers

TypeORM SyntaxError: Unexpected token import


問題

(node:6560) UnhandledPromiseRejectionWarning: c:\workspace\liberapp.net\src\entities\application.ts:1

(function (exports, require, module, __filename, __dirname) { import {
^^^^^^

SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Function.PlatformTools.load (c:\workspace\liberapp.net\node_modules\typeorm\platform\PlatformTools.js:135:28)
(node:6560) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6560) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[20:27:47] [nodemon] clean exit - waiting for changes before restart


発生原因

こちらの不具合修正のために下記の対応を行ったのが原因


orgmconfig.js

const config = require("config");

module.exports = {
name: "default",
type: "mysql",
username: config.mysql.user,
...config.mysql,
synchronize: false,
logging: false,
- entities: ["dist/entities/**/*.js"],
- migrations: ["dist/migrations/**/*.js"],
+ entities: ["src/entities/**/*.ts"],
+ migrations: ["src/migrations/**/*.ts"],
subscribers: ["src/subscribers/**/*.ts"],
migrationsRun: true,
cli: {
entitiesDir: "src/entities",
migrationsDir: "src/migrations",
subscribersDir: "src/subscribers"
}
};



該当するgulp


gulp

const NODEMON_CONFIG = {

script: "./dist/www",
env: { NODE_ENV: "development", DEBUG: "*:*" },
delay: 3000
};

gulp.task("watch", () => {
gulpWatch(
[
paths.serverTypescriptSrc,
paths.commonTypescriptSrc,
paths.serverLocalesSrc,
paths.serverViews
],
gulp.series("build:server")
);
gulpWatch(paths.clientTypescriptSrc, gulp.series("build:client"));
});

gulp.task("start-nodemon", function() {
nodemon(NODEMON_CONFIG);
});

gulp.task(
"start-dev",
gulp.series("build", gulp.parallel("watch", "start-nodemon"))
);



気になる点


  1. nodemonがJavaScriptコードを実行しているので直接TypeScriptを実行したい

  2. nodemonとwatchタスクが多重になっていないか?


対応1


gulp

const NODEMON_CONFIG = {

+ watch: ["./src"],
+ ext: "ts",
+ exec: "ts-node ./src/www.ts",
- // script: "./dist/www",
env: { NODE_ENV: "development", DEBUG: "*:*" },
delay: 3000
};


結果

先ほどのエラーは消えました

こんどはrouting-controllerで設定しているコントローラが効かなくなった


最終対応

こちらのIssueをみつけた

https://github.com/typestack/routing-controllers/issues/171

ディレクトリベースでコントローラを読み込んでいるが拡張子jsになっていたがts-nodeで直接起動しているのでtsにする


app.ts

  private get routingControllerOptions(): RoutingControllersOptions {

return {
- controllers: [`${__dirname}/controllers/*.js`]
+ controllers: [`${__dirname}/controllers/*.ts`]
};
}