ちょいハマりした。
コイツ。
(node:93711) UnhandledPromiseRejectionWarning: /Users/username/project/src/entity/SomeEntity.ts:1
(function (exports, require, module, __filename, __dirname) { import {
^
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
(node:93711) 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:93711) [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.
こういうエラーがいちばん嫌い。
環境
typescript ^3.3.3333
typeorm ^0.2.14
経緯
typeormセットアップして、
それ用のEntityを作って、
tscして、
node dist/index.js
したら上記エラーが出た。
エラー内容的にはNodeに対応してない文法が存在しているって感じだけど、そもそもtsからjsにコンパイル済みだし、意味不明。
typescriptのコンフィグにもちゃんと下記は入れてる。
"module": "commonjs",
"target": "es6",
意味わかんねーよバーカ!
結論
typeormのドキュメントが分かりづらい。
typeormを利用する際、下記のようなconfigがプロジェクトルートに必要になる。
(厳密に言うと必須ではない。コード内に書くこともできるので)
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "hogerahogera",
"database": "mogeramogera",
"synchronize": true,
"logging": false,
"entities": [
"src/entity/**/*.ts"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
]
}
で、entitiesプロパティがプロジェクト内のEntity(他で言うmodel。要はテーブルを表す)の場所を指す。
のだが、僕のようにコンパイルしてからコンパイル後のファイルをnodeで動かしてる人の場合、上記の設定のままだとコンパイル後のjsファイルから、コンパイル前のtsファイルであるentityを読みに行ってしまう。
そんなん知るかよって感じ。
まあ徹頭徹尾ドキュメントを読んでない僕が悪いっちゃ悪いのだが、もうちょっと書きようはあると思う。
一応それっぽいことは、ドキュメント内のここに書いてあった。
修正後はこんな感じになります。
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "hogerahogera",
"database": "mogeramogera",
"synchronize": true,
"logging": false,
"entities": [
"dist/entity/**/*.js" <- ここが変わってる
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
]
}
うまく動いた!
typescriptになれてる人ならハマらないのかな?
こういうconfig系で何度も躓くので、実践的なtsプロジェクトの作り方とか落ち着いたらまとめます。