typeorm: Migration Unexpected token error

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I’m having a problem when running typeorm migration:run I’m getting this error:

(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column } from ‘typeorm’;
SyntaxError: Unexpected token {

After reading a few issues people suggested to run: ts-node ./node_modules/.bin/typeorm migration:run, but I’m also getting errors:

basedir=$(dirname “$(echo “$0” | sed -e ‘s,\,/,g’)”) ^^^^^^^ SyntaxError: missing ) after argument list

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 22 (6 by maintainers)

Most upvoted comments

I solved my problem by installing ts-node as a dev dependency:

npm i -D ts-node

and later by adding script to package.json file:

{
  /* ... */
  "scripts": {
    /* ... */
    "typeorm": "ts-node ./node_modules/.bin/typeorm"
  }
}

Now you can invoke any typeorm CLI command in the following way:

npm run typeorm -- migration:generate -n MigrationName

Guys, I believe this problem happen when typeorm migration:run tries to execute .ts files. Try to change your ormconfig and specify only .js files in there.

I solved my problem by installing ts-node as a dev dependency:

npm i -D ts-node

and later by adding script to package.json file:

{
  /* ... */
  "scripts": {
    /* ... */
    "typeorm": "ts-node ./node_modules/.bin/typeorm"
  }
}

Now you can invoke any typeorm CLI command in the following way:

npm run typeorm -- migration:generate -n MigrationName

I tried that and even changed the script to mine that was working (ts-node ./node_modules/typeorm/cli.js) and typed npm run typeorm with no lock. the scipt it tries to run is ts-node ./node_modules/typeorm/cli.js "migration:generate" "-n" "MigrationName" why put them in quotations ???

Update: solved by removing "esModuleInterop": true, from tsconfig but still, I have to put all script in scripts section I mean npm run typeorm -- migration:generate -n MigrationName translated to ts-node ./node_modules/typeorm/cli.js "migration:generate" "-n" "MigrationName" thanks

I’m using migration with .ts files and it works without any problems.

// package.json->scripts
"db:migrate": "npx ts-node ./node_modules/.bin/typeorm migration:run -f ormconfig-cli",
// ormconfig-cli.ts
import { SnakeNamingStrategy } from './src/database/snake-naming.strategy';

module.exports = {
   type: 'mysql',
   host: 'localhost',
   port: 3306,
   username: 'root',
   password: '123456789',
   database: 'mydb',
   synchronize: false,
   logging: true,
   maxQueryExecutionTime: 100,
   namingStrategy: new SnakeNamingStrategy(),
   entities: ['src/database/entity/**/*.ts'],
   migrations: ['src/database/migration/**/*.ts'],
   subscribers: ['src/database/subscriber/**/*.ts'],
   cli: {
      entitiesDir: 'src/database/entity',
      migrationsDir: 'src/database/migration',
      subscribersDir: 'src/database/subscriber'
   }
};

after long I installed ts-node typescript typeorm globally and use ts-node ./node_modules/typeorm/cli.js migration:generate -n new1 command things start to work. Update: I added "esModuleInterop": true, to tsconfig.json

I solved my problem by installing ts-node as a dev dependency:

npm i -D ts-node

and later by adding script to package.json file:

{
  /* ... */
  "scripts": {
    /* ... */
    "typeorm": "ts-node ./node_modules/.bin/typeorm"
  }
}

Now you can invoke any typeorm CLI command in the following way:

npm run typeorm -- migration:generate -n MigrationName

I tried that and even changed the script to mine that was working (ts-node ./node_modules/typeorm/cli.js) and typed npm run typeorm with no lock. the scipt it tries to run is ts-node ./node_modules/typeorm/cli.js "migration:generate" "-n" "MigrationName" why put them in quotations ???

Update: solved by removing "esModuleInterop": true, from tsconfig but still, I have to put all script in scripts section I mean npm run typeorm -- migration:generate -n MigrationName translated to ts-node ./node_modules/typeorm/cli.js "migration:generate" "-n" "MigrationName" thanks

.\node_modules\.bin\ts-node ./node_modules/typeorm/cli.js migration:generate -n MigrationName worked for me. I’ve installed ts-node locally.