typeorm: [question] migration cannot recongnize keyword `import`?

After setting up ormconfig.json, I use typeorm migrations:create -n base to create migration file. But when I run typeorm migrations:run, it seems cannot recognize the key word import.

ormconfig.json

{
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "dinfer",
    "password": "dinfer",
    "database": "haodf",
    "logging": {
        "logOnlyFailedQueries": true
    },
    "autoSchemaSync": false,
    "migrations": [
        "src/models/migrations/*.ts"
    ],
    "cli": {
        "migrationsDir": "src/models/migrations"
    }
}

the migration script

import { ColumnSchema, MigrationInterface, QueryRunner, TableSchema } from 'typeorm';

export class IncreaseDoctorSkillLength1501091244552 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.createTable(new TableSchema('a', [
            new ColumnSchema({ name: 't', type: 'string', length: 1000 })
        ]))
    }

    public async down(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.dropTable('a')
    }

}

the output

Error during migration run:
E:\crawler\spider\src\models\migrations\1501091244552-base.ts:1
(function (exports, require, module, __filename, __dirname) { import { ColumnSchema, MigrationInterface, QueryRunner, TableSchema } from 'typeorm';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Function.PlatformTools.load (C:\Users\dinfer\AppData\Roaming\npm\node_modules\typeorm\platform\PlatformTools.js:28:20)
PS E:\work\crawler\spider>

tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "rootDir": "./src",
    "strict": true,
    "experimentalDecorators": true, 
    "emitDecoratorMetadata": true
  }
}

npm ls -g --depth=0

+-- typeorm@0.1.0-alpha.32

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 54 (27 by maintainers)

Most upvoted comments

Had same problem, solved by having: "migrate": "ts-node ./node_modules/.bin/typeorm migrations:generate" in the scripts section of package.json and then doing: npm run migrate -- --name InitialMigration

I agree. You need to use ts-node. The problem for me is both ./node_modules/.bin/typeorm and ./node_modules/.bin/typeorm.cmd fail to generate/run/revert the migrations. I can create an empty migration using typeorm migrations:create without any issues.

My work-around is accessing the typeorm cli.js directly using package json, like so… "migrations:generate": "ts-node ./node_modules/typeorm/cli.js migrations:generate", "migrations:run": "ts-node ./node_modules/typeorm/cli.js migrations:run", "migrations:revert": "ts-node ./node_modules/typeorm/cli.js migrations:revert" Then running the migrations like so… yarn migrations:generate --name BaseMigration yarn migrations:run yarn migrations:revert This is working for me on Windows10, using VSCode and git-bash terminal. I am enjoying working with TypeORM, I like it.

You can execute any node app using ts-node:

ts-node ./node_modules/.bin/typeorm

This way it will compile ts files on the fly.

This should be part of ts-node documentation, but yeah its good to mention about it in typeorm documentation too.

@martinsura this worked for me. Added script in package.json "migrate": "ts-node ./node_modules/typeorm/cli.js migrations:generate" Then generated migration like so… yarn migrate -- --name BaseMigration

If I use "migration:generate": "ts-node ./node_modules/typeorm/cli.js migration:generate" in package.json I get the error message “missing argument n”.

These scripts are working for me:

  "migration:generate": "ts-node ./node_modules/typeorm/cli.js migration:generate -n",

  "migration:run": "ts-node ./node_modules/typeorm/cli.js migration:run",

  "migration:revert": "ts-node ./node_modules/typeorm/cli.js migration:revert"

Then in commandline: npm run migration:generate MigrationName

For anyone else having issues using ts-node in yarn workspaces or lerna. Just make a file called tsnode-typeorm.js:

require('ts-node').register({ typeCheck: false });
require('typeorm/cli');

To run migrations run node tsnode-typeorm migrations:run

Guys to summarize, TypeORM uses TypeScript to enhance javascript and enable a better way of declaring your schema, however it doesn’t give you TS to JS compilation nor a middleware for it’s execution directly in TS, so when you execute TypeORM, you NEED to use ts-node by prepending it to all commands used in console (though it’s easier to hard code such commands in npm scripts in package.json).

About @gmongin issue, the following line is a bash command, i doubt it would work in windows cmd or powershell, maybe try installing git-bash, babun (i like it, but some issues with yarn in the most recent version), or some other bash emulator:

basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

Lastly i know packages which accept TS files, like Gulp, they accept their config file to be made in TS, and they will tap into ts-node if installed, i asked before if that can be done here as well, but most people said it’s not a priority, and prepending the TypeORM command with ts-node is a good enough workaround, what a shame, i feel that most newcomers bump into this and creates a high barrier of entry.

I agree. You need to use ts-node. The problem for me is both ./node_modules/.bin/typeorm and ./node_modules/.bin/typeorm.cmd fail to generate/run/revert the migrations. I can create an empty migration using typeorm migrations:create without any issues.

My work-around is accessing the typeorm cli.js directly using package json, like so… "migrations:generate": "ts-node ./node_modules/typeorm/cli.js migrations:generate", "migrations:run": "ts-node ./node_modules/typeorm/cli.js migrations:run", "migrations:revert": "ts-node ./node_modules/typeorm/cli.js migrations:revert" Then running the migrations like so… yarn migrations:generate --name BaseMigration yarn migrations:run yarn migrations:revert This is working for me on Windows10, using VSCode and git-bash terminal. I am enjoying working with TypeORM, I like it.

Just want to help others who also looked into this solution, now the migration commands are Singular, eg: "migration:generate": "ts-node ./node_modules/typeorm/cli.js migration:generate",

typeorm Version I am using: “~0.2.7”

try to run js files instead of ts, e.g.:

    "migrations": [
        "src/models/migrations/*.js" // make sure to specify outdir if you are using outdir
    ]

“missing argument n”

Oh my goodness, if anyone else ever comes across this thread, and has the error message: “Missing required argument: n”

After running a command like this:

npm run typeorm:cli -- migration:create -n UserFullName

image

The issue was PowerShell!

Look in the screenshot above how it’s removed the -n flag on the command

Switching to cmd or Git Bash solved this for me.

If you are using ormconfig.json file, it should look like:

{
    "type": "postgres",
    "host": "localhost",
    "port": 5432,
    "username": "root",
    "password": "password",
    "database": "persona_db",
    "entities": [
        "dist/**/*.entity{.ts,.js}"
    ],
    "synchronize": false,
    "migrations": [
        "dist/migrations/**/*{.ts,.js}"
    ],
    "cli": {
        "migrationsDir": "migrations"
    }
}

NOTE: Please refer to the “dist” folder and not the root folder…

@aluco100 migration generation is supported only in mysql right now, we are planning to release its support in sqlite in 0.2.0