Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot use import statement outside a module" in typeorm migration when run nestjs app

Tags:

typeorm

nestjs

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?

like image 890
Darek K. Avatar asked Apr 16 '20 20:04

Darek K.


3 Answers

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 :)

like image 126
Tiago Marmitt Avatar answered Nov 09 '22 10:11

Tiago Marmitt


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",
like image 4
teteArg Avatar answered Nov 09 '22 10:11

teteArg


I had the same issue. I did the following as a workaround:

  1. Set your ormconfig.json to look for migrations files with only js suffix:
    "migrations": ["migrations/*.js"],
  2. Install typescript globally: npm install -g typescript
  3. After generating your migration, transpile typescript files to javascript files with typescript command:
    tsc migrations/migration-file.ts
  4. Run your migration: npm run typeorm migration:run
  5. You can now run your application: npm run start:dev
like image 2
BrainKode Avatar answered Nov 09 '22 08:11

BrainKode