'"Cannot use import statement outside a module" in typeorm migration when run nestjs app
I have created the nestjs app. In the root app folder I have these subfolders:
- dist
- migration
- src
- test
The migration folder contains typeorm migrations.
When run application with npm run start:dev I have this error:
import {MigrationInterface, QueryRunner} from "typeorm";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:891:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Module.require (internal/modules/cjs/loader.js:848:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Function.PlatformTools.load (C:\Users\dakru1\Documents\employo\employo-api\node_modules\typeorm\platform\PlatformTools.js:114:28)
at C:\Users\dakru1\Documents\employo\employo-api\node_modules\typeorm\util\DirectoryExportedClassesLoader.js:39:69
at Array.map (<anonymous>)
at Object.importClassesFromDirectories (C:\Users\dakru1\Documents\employo\employo-api\node_modules\typeorm\util\DirectoryExportedClassesLoader.js:39:10)
I understand the error message and I know how to fix it when it relates to application's code.
However, my problem is that this error come from typeorm migration file: [app-root-folder]\migration\1587067680466-Init.ts
which shouldn't be used when application runs.
Why nestjs uses migration files. How can I ignore migration folder when running nestjs app?
Solution 1:[1]
to solve it just put the following code on your "scripts" at package.json:
"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js",
After that you'll be able to run your typeorm migration:run :)
Solution 2:[2]
I had the same issue. I did the following as a workaround:
- Set your ormconfig.json to look for migrations files with only
js
suffix:"migrations": ["migrations/*.js"]
, - Install typescript globally:
npm install -g typescript
- After generating your migration, transpile typescript files to javascript files with typescript command:
tsc migrations/migration-file.ts
- Run your migration:
npm run typeorm migration:run
- You can now run your application:
npm run start:dev
Solution 3:[3]
I found the solution:
This happends to me because I was using Nest and when you run the command nest start
, under the hood, Nest executes node and tsc and node commands and because of that you need your files to be in JavaScript format. So, as the migrations are generated in Typescript... the statement import gives the error because is only for TypeScript.
The only thing i did to solve it is this:
migrations: ["dist/migrations/*.js"],
This is telling TypeOrm that search for the migration in the dist
directory where all the JavaScript code was compiled previously.
Of course in my tsconfig.json
file the output dir is pointing to dist
.
"outDir": "./dist",
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tiago Marmitt |
Solution 2 | BrainKode |
Solution 3 |