6
votes

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?

2

2 Answers

5
votes

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

1
votes

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