Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration cli unexpected token import #371

Closed
adithep opened this issue Mar 26, 2017 · 29 comments
Closed

Migration cli unexpected token import #371

adithep opened this issue Mar 26, 2017 · 29 comments
Labels

Comments

@adithep
Copy link

adithep commented Mar 26, 2017

I wrote everything in typescript.
The application and everything work fine, but as soon as I call

typeorm migrations:run

I get this error

src/entity/User.ts:1
(function (exports, require, module, __filename, __dirname) { import {Entity, Column, PrimaryGeneratedColumn} from "typeorm";
^^^^^^
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:78:16)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Function.PlatformTools.load (/usr/local/lib/node_modules/typeorm/platform/PlatformTools.js:26:20)
at /usr/local/lib/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:29:69
(node:38469) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 5): SyntaxError: Unexpected token import
(node:38469) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Not sure if it matters but this is my tsconfig.json:

{
"compilerOptions": {
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"isolatedModules": false,
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"sourceMap": true,
"sourceRoot": "src",
"noEmitHelpers": false,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": ["es6", "dom"]
},
"exclude": [
"node_modules"
]
}

What ever I change in tsconfig.json seems to not effect the error at all.

@pleerock
Copy link
Member

you node environment tries to load src/entity/User.ts, but it should load .js. So you either need to run app with ts-node, or set proper configuration for JS FILES in your ormconfig cli section, e.g. dist/migrations/.*js

@pleerock
Copy link
Member

pleerock commented Apr 1, 2017

did you resolve your issue?

@adithep
Copy link
Author

adithep commented Apr 3, 2017 via email

@SaeedSyedAli96
Copy link

@adithep how did you resolve the issue?

@stormpat
Copy link

Anyone wondering about this, you must run your .ts files thru ts-node first.

Simply:

>> ./node_modules/.bin/ts-node ./node_modules/.bin/typeorm <command>

@davidmpaz
Copy link

Hi @stormpat,

thanks for the hint. Still is not working for me.

./node_modules/.bin/ts-node ./node_modules/.bin/typeorm schema:log

gives me still:

import { Column, Entity, ManyToOne } from 'typeorm';
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Module.m._compile (/srv/http/is7/mobile-sdk/src/modules/products/node_modules/ts-node/src/index.ts:422:23)
    at Module._extensions..js (module.js:654:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/srv/http/is7/mobile-sdk/src/modules/products/node_modules/ts-node/src/index.ts:425:12)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)

Any idea ?

@pleerock
Copy link
Member

@davidmpaz try to setup your tsconfig properly - try to set target to es5

@davidmpaz
Copy link

Hi @pleerock ,

thanks for answering. These are my current config files

tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "noStrictGenericChecks": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

ormconfig.json

{
    "type": "sqlite",
    "database": "./products.db",
    "synchronize": true,
    "logging": false,
    "entities": [
        "src/models/**/*.ts"
    ],
    "migrations": [
        "src/migration/**/*.ts"
    ],
    "subscribers": [
        "src/subscriber/**/*.ts"
    ],
    "cli": {
        "entitiesDir": "src/models",
        "migrationsDir": "src/migration",
        "subscribersDir": "src/subscriber"
    }
}

package.json deps:

"dependencies": {
        "@angular/common": "4.1.3",
        "@angular/core": "4.1.3",
        "@angular/forms": "4.1.3",
        "@angular/http": "4.1.3",
        "ionic-angular": "^3.6.0",
        "typeorm": "0.1.11",
        "reflect-metadata": "^0.1.10",
        "ngx-order-pipe": "^1.1.0",
        "sqlite3": "3.1.13",
        "rxjs": "5.4.0"
    },
    "devDependencies": {
        "tslint": "^5.8.0",
        "tslint-eslint-rules": "^4.1.1",
        "ts-node": "4.1.0",
        "typescript": "^2.6.1"
    }

I would like to see what get generated with my current entity models definitions, was trying to do 'insitu'. I guess I will try to generate a project from scratch and copy paste entities.

But would be nice if I can get it up running in my existent project.

thanks in advance for your time,
David

@davidmpaz
Copy link

It seems my problem is known here, related to my settings: "module": "es2015"

Sorry to introduce this noise.

Thanks!!

@91debug
Copy link

91debug commented Jan 25, 2018

@davidmpaz how did you resolve the issue?

These are my current config files
tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "rootDir": "./",
    "outDir": "./tsc",
    "noImplicitAny": false,
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "declaration": false,
    "listFiles": false,
    "lib": [
      "es6",
      "dom",
      "es2015.promise",
      "esnext.asynciterable"
    ],
    "noUnusedLocals": true,
  },
  "exclude": [
    ".vscode",
    ".serverless",
    ".git",
    "node_modules"
  ],
  "compileOnSave": true,
  "buildOnSave": false
}

@davidmpaz
Copy link

@91debug currently I haven't.

@91debug
Copy link

91debug commented Jan 25, 2018

@davidmpaz i resolved! thank you

@davidmpaz
Copy link

@91debug, could you share ? :)

@91debug
Copy link

91debug commented Jan 25, 2018

@davidmpaz

I used this command
./node_modules/.bin/ts-node ./node_modules/.bin/typeorm schema:sync

@tonyxiao
Copy link

it's surprising that typeorm cli doesn't work with typescript by default without asking user to explicitly pass through ts-node. Is that something which could be built in given how typeorm itself is written in typescript?

@SaeedSyedAli96
Copy link

SaeedSyedAli96 commented Feb 16, 2018

I found a way to make migratons work with my ionic 3 application.
NO NEED TO EXPLICITLY PASS TS-NODE
In ormconfig.json:

{
    "cli": {
        "migrationsDir": "src/migrations/"
    }
}

and the connection:

 await createConnection({
            type: 'cordova',
            database: 'test',
            location: 'default',
            logging: ['error', 'query', 'schema'],
            synchronize: false,
            entities: [
                //entities
            ],
            migrations:[
                //migrations generated by typeorm cli
            ],
            migrationsRun: true
        }).catch(err => console.log("error: ", err));

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "./node_modules/@types"
    ]
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "src/**/*.spec.ts",
    "src/**/__tests__/*.ts"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

@tonyxiao
Copy link

@SaeedSyedAli96 how does it work?

@SaeedSyedAli96
Copy link

@tonyxiao I just used migration cli to generate migrations as it needs timestamp. Else Entitites and and other stuff I did manually. I was having issues in running migration from cli but then I got to know that there is an option of migrationsRun as well.

@mindonline
Copy link

Also, in Win 10 works:
ts-node node_modules\typeorm\cli.js

@eightHundreds
Copy link

eightHundreds commented May 3, 2018

for es project in win 10

.\node_modules\.bin\babel-node .\node_modules\typeorm\cli.js schema:sync

@bennycode
Copy link

I had a similar issue when running my JavaScript application code (which has been transpiled from TypeScript):

(function (exports, require, module, __filename, __dirname) { import {Column, Entity, PrimaryGeneratedColumn} from "typeorm";

SyntaxError: Unexpected token {

I solved it by pointing the entities in my ormconfig.js to my compiled JavaScript code. Before that (when I had the error) they have been linked to my TypeScript source code.

This configuration now works for me:

ormconfig.js

const ConnectionOptions = {
  cli: {
    entitiesDir: 'src/main/entity',
    migrationsDir: 'src/migration',
    subscribersDir: 'src/subscriber',
  },
  database: 'database/development.db3',
  entities: ['dist/entity/*.js'],
  logging: false,
  migrations: ['src/migration/**/*.ts'],
  subscribers: ['src/subscriber/**/*.ts'],
  synchronize: true,
  type: 'sqlite',
};

if (process.env.NODE_ENV === 'production') {
  ConnectionOptions.database = 'database/production.db3';
}

if (process.env.NODE_ENV === 'test') {
  ConnectionOptions.database = 'database/test.db3';
}

module.exports = ConnectionOptions;

@AhmadDeel
Copy link

AhmadDeel commented Dec 24, 2018

after long I installed ts-node and typescript globally and use
ts-node ./node_modules/typeorm/cli.js migration:generate -n MigrationName command things start to work.
then added these to package.json

{
  /* ... */
  "scripts": {
  /* ... */
    "typeorm-migration-generate": "ts-node ./node_modules/typeorm/cli.js migration:generate -n MigrationName",
    "typeorm-migration-run": "ts-node ./node_modules/typeorm/cli.js migration:run",
    "typeorm-migration-revert": "ts-node ./node_modules/typeorm/cli.js migration:revert"
  }
}

now i can just run npm run typeorm-migration-run and others

@2234839
Copy link

2234839 commented Mar 21, 2019

"entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],

我遇到了同样 的 问题在将 ormconfig.json中的 ts 改为 js之后解决了问题

@vngrv
Copy link

vngrv commented Sep 5, 2019

It seems my problem is known here, related to my settings: "module": "es2015"

Sorry to introduce this noise.

Thanks!!

this changes killed my build.

@rpmonteiro
Copy link

Using ENV variables, the fix for me was

TYPEORM_ENTITIES=dist/**/*.entity.js
TYPEORM_MIGRATIONS=dist/migration/*.js
TYPEORM_MIGRATIONS_DIR=dist/migration

Hope this helps someone

@willianfalbo
Copy link

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...

@zeroliu
Copy link

zeroliu commented May 29, 2020

@willianfalbo I ran into a similar issue with deploying the compiled js code to server. I was very confused how any ts file was referenced to the compiled code. Didn't even think about checking ormconfig.json...Thanks so much!!!

@gitDylanHub
Copy link

Hi @stormpat,

thanks for the hint. Still is not working for me.

./node_modules/.bin/ts-node ./node_modules/.bin/typeorm schema:log

gives me still:

import { Column, Entity, ManyToOne } from 'typeorm';
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Module.m._compile (/srv/http/is7/mobile-sdk/src/modules/products/node_modules/ts-node/src/index.ts:422:23)
    at Module._extensions..js (module.js:654:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/srv/http/is7/mobile-sdk/src/modules/products/node_modules/ts-node/src/index.ts:425:12)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)

Any idea ?
Another workaround for executing the TypeORM from a NPM Scritp.
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
"migration:generate": "npm run typeorm migration:generate -- -n",

@IgnisDa
Copy link

IgnisDa commented Sep 2, 2021

My solution was to install typescript and ts-node globally and use them instead of the node_modules

sudo npm install -g typescript ts-node
ts-node ./node_modules/typeorm/cli.js migration:generate -n FirstMigration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests