LoginSignup
7
0

More than 1 year has passed since last update.

TypeORMのMigrationでCannot use import statement outside a moduleエラーの対処法

Last updated at Posted at 2021-10-06

TL;DR

あまり綺麗ではないですが、これで解決しました。
typeormコマンドを使用せず、直接ts-nodetypeormを呼び出します。

$ ts-node ./node_modules/typeorm/cli.js migration:generate -n 'User'

NestJSを使用しているときにこのエラーに遭遇した場合は、ページ下部のormconfig.jsonの書き換えを試してみてください。

よくみたらドキュメントに書いてあった...

ドキュメントの推奨している方法だと、、、

STEP 1

ts-nodeをグローバルにインストール

$ npm install -g ts-node

STEP 2

package.jsonにスクリプトを書き加える

"scripts": {
    ...
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"    
}

STEP 3

そしたら、こんな感じで実行する
--は必要なので注意!

$ npm run typeorm migration:generate -- -n UserMigration

遭遇したエラー内容

$ npx typeorm init
$ npm install
$ typeorm migration:generate -n UserMigration

Error during migration generation:
/Users/michinosuke/archive/web-app/typeorm-playground/src/entity/role.ts:1
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:984:16)
    at Module._compile (internal/modules/cjs/loader.js:1032:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at /usr/local/lib/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:42:39
    at Array.map (<anonymous>)
    at Object.importClassesFromDirectories (/usr/local/lib/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:42:10)

上のやつで解決しないとき

一応、他の可能性も考えてみます。

ts-nodeも直接パスを指定する

ts-nodeがグローバルにインストールされていない場合は、これで解決するかも。

$ ./node_modules/.bin/ts-node ./node_modules/typeorm/cli.js migration:generate -n 'User'

環境変数をつける

  • TS_NODE_PROJECTtsconfig.jsonのパスを渡します。
  • TS_NODE_TRANSPILE_ONLYtrueを指定して、TypeScriptのtranspileModuleを使ってみる。

transpileModuleについての参考

TS_NODE_PROJECT=tsconfig.json TS_NODE_TRANSPILE_ONLY=true ./node_modules/.bin/ts-node ./node_modules/typeorm/cli.js migration:generate -n 'User'

tsconfig.jsonを書き換える

compilerOptionsmodulecommonjs以外になっているときは、commonjsに変更してみてください。

tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs"
  }
}

さらに他の解決法

一旦TypeScriptで.tsファイルを.jsにトランスパイルしちゃう方法です。綺麗な解決策ではありませんが、ほぼ確実に解決できると思います。

もしあなたがNestJSなどを使用してこのエラーに遭遇している場合、下の手順3ormconfig.jsonの書き換えだけで解決する可能性が高いです。

手順1

tsconfig.jsoncompilerOptionsskipLibChecktrueを指定します。
あまりいい方法じゃないんですが、これを指定しないとトランスパイルにエラーが大量に出ます。

参考: ERROR TS1086: An accessor cannot be declared in an ambient context

{
   "compilerOptions": {
      "skipLibCheck": true
   }
}

手順2

トランスパイルします。

$ tsc

上のコマンドが打てなかったら、TypeScriptをグローバルインストールしてください。

$ npm i -g typescript

実行できたら、プロジェクトルートにbuildディレクトリが作成されて、jsファイルがたくさん入ってるはずです。

手順3

ormconfig.jsonを以下のように書き換えます。
* src/build/
* .ts.js

Before

{
   "type": "sqlite",
   "database": "database.sqlite",
   "synchronize": true,
   "logging": false,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}

After

{
   "type": "sqlite",
   "database": "database.sqlite",
   "synchronize": true,
   "logging": false,
   "entities": [
      "build/entity/**/*.js"
   ],
   "migrations": [
      "build/migration/**/*.js"
   ],
   "subscribers": [
      "build/subscriber/**/*.js"
   ],
   "cli": {
      "entitiesDir": "build/entity",
      "migrationsDir": "build/migration",
      "subscribersDir": "build/subscriber"
   }
}

手順4

nodetypeormを呼び出します。

$ node ./node_modules/typeorm/cli.js migration:generate -n 'User'

参考

7
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
0