typeorm: Migration cli unexpected token import
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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 8
- Comments: 29 (3 by maintainers)
Anyone wondering about this, you must run your .ts files thru ts-node first.
Simply:
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?
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/.*jsAlso, in Win 10 works:
ts-node node_modules\typeorm\cli.jsafter long I installed ts-node and typescript globally and use
ts-node ./node_modules/typeorm/cli.js migration:generate -n MigrationNamecommand things start to work. then added these to package.jsonnow i can just run
npm run typeorm-migration-runand others@davidmpaz
I used this command ./node_modules/.bin/ts-node ./node_modules/.bin/typeorm schema:sync
for es project in win 10
If you are using
ormconfig.jsonfile, it should look like:NOTE: Please refer to the “dist” folder and not the root folder…
Hi @stormpat,
thanks for the hint. Still is not working for me.
gives me still:
Any idea ?
did you resolve your issue?
我遇到了同样 的 问题在将 ormconfig.json中的 ts 改为 js之后解决了问题
I found a way to make migratons work with my ionic 3 application. NO NEED TO EXPLICITLY PASS TS-NODE In ormconfig.json:
and the connection:
tsconfig.json
@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!!!
Using ENV variables, the fix for me was
Hope this helps someone
I had a similar issue when running my JavaScript application code (which has been transpiled from TypeScript):
I solved it by pointing the
entitiesin myormconfig.jsto 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