Search code examples
node.jsjsontypescripttypeorm

TypeORM generates an empty migration


Whenever I run typeorm migration:generate -n NAME, all I get is an error stating I that no changes to the database were made. Whenever I run typeorm migration:create -n NAME, I get an empty migration file. All of my entities are in the folder specified in the ormconfig.json file, and are in the .ts format. When running a migration:generate command, I get an error that is related to the syntax in my entities (specifically where I have my imports on top of the file).

This is my ormconfig.json:

{
  "name": "default",
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "postgres",
  "password": "admin",
  "database": "classmarker",
  "synchronize": true,
  "logging": false,
  "entities": [
    "src/entity/*.ts"
  ],
  "subscribers": [
    "src/subscriber/*.ts"
  ],
  "migrations": [
    "src/migration/*.ts"
  ],
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
  }
}

My package.json contains the following packages:

  "dependencies": {
    "@tsed/common": "^5.21.0",
    "@tsed/core": "^5.21.0",
    "@tsed/di": "^5.21.0",
    "@types/mssql": "^4.0.15",
    "@types/node": "^12.0.12",
    "body-parser": "^1.19.0",
    "compression": "^1.7.4",
    "concurrently": "^4.1.1",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-handlebars": "^3.1.0",
    "method-override": "^3.0.0",
    "reflect-metadata": "^0.1.12",
    "pg": "^7.11.0",
    "typeorm": "^0.2.15"
  },
  "devDependencies": {
    "@types/express": "^4.17.0",
    "@types/node": "^9.6.5",
    "dotenv": "^8.0.0",
    "nodemon": "^1.19.1",
    "ts-node": "^3.3.0",
    "typescript": "^3.3.3333"
  }

And my tsconfig.json looks like this:

{
    "version": "2.4.2",
    "compilerOptions": {
        "lib": ["es5", "es6"],
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true
    },
    "exclude": [
        "node_modules"
    ]
}

The error I get when running typeorm migration:generate -n Name:

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Function.PlatformTools.load (%AppData%\nvm\v8.11.2\node_modules\typeorm\platform\PlatformTools.js:107:28

Solution

  • Unexpected token import tends to show up when you try to generate or run a migration that is in the .ts format (I presume that it shows up due to you trying to import something at the top of your .ts file). Since TypeORM works properly with .js instead of .ts (don't ask why), try running ts-node ./node_modules/typeorm/cli.js migration:generate -n NAME to generate a migration and ts-node ./node_modules/typeorm/cli.js migration:run to push it to the database instead.

    Essentially, it's easier to add something like this into your package.json:

        "add-migration": "ts-node ./node_modules/typeorm/cli.js migration:generate -n",
        "update-database": "ts-node ./node_modules/typeorm/cli.js migration:run"
    

    Then you can simply run them using npm run add-migration -n NAME and npm run update-database.

    typeorm migration:create and typeorm migration:generate will create ts files. The migration:run and migration:revert commands only work on .js files. Thus the typescript files need to be compiled before running the commands. Alternatively you can use ts-node in conjunction with typeorm to run .ts migration files.