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)
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 InitialMigrationI 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 BaseMigrationyarn migrations:runyarn migrations:revertThis 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:
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 BaseMigrationIf 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:
Then in commandline:
npm run migration:generate MigrationNameFor anyone else having issues using ts-node in yarn workspaces or lerna. Just make a file called
tsnode-typeorm.js:To run migrations run
node tsnode-typeorm migrations:runGuys 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-nodeby prepending it to all commands used in console (though it’s easier to hard code such commands innpm scriptsinpackage.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:
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-nodeif 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 withts-nodeis a good enough workaround, what a shame, i feel that most newcomers bump into this and creates a high barrier of entry.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
jsfiles instead ofts, e.g.: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 UserFullNameThe issue was PowerShell!
Look in the screenshot above how it’s removed the
-nflag on the commandSwitching to
cmdor Git Bash solved this for me.If you are using
ormconfig.jsonfile, it should look like: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