I have one project using typeorm to create the database. And when I execute the command to generate a migration to my project, it gives me the error
I have the following orm configuration json file:
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "",
"database": "postgres",
"synchronize": true,
"logging": false,
"entities": [
"src/entity/**/*.ts"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
I have this User entity class:
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from "typeorm";
import { Transaction } from "./Transactions";
export type UserStatus = "Active" | "Inactive";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column("varchar",{
length:50
})
account_id: string;
@Column("varchar",{
length: 50
})
name: string;
@Column("varchar",{
length:50
})
email: string;
@Column("varchar",{
length:15
})
phone_number: string;
@Column("varchar",{
length:50
})
address: string;
@Column({
type: "enum",
enum: ["Active", "Inactive"]
}) status: UserStatus;
@Column("varchar",{
length:50
})
current_id: string;
@OneToMany(type => Transaction, transaction => transaction.user)
transactions: Transaction[];
}
If I execute the command to generate a migration, it gives me the following error:
C:\Users\Firdaus97\source\repos\Cust Local payment\Cust Local payment\MyProject>ts-node ./node_modules/.bin/typeorm migrations:generate -n
C:\Users\Firdaus97\source\repos\Cust Local payment\Cust Local payment\MyProject\node_modules\.bin\typeorm:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at Module._compile (internal/modules/cjs/loader.js:720:22)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:840:10)
at Object.<anonymous> (C:\Users\Firdaus97\AppData\Roaming\npm\node_modules\ts-node\src\bin.ts:158:12)
at Module._compile (internal/modules/cjs/loader.js:777:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
I had this issue on Windows 10, and was able to fix it as per this answer and this comment:
In my package.json scripts I earlier had
"scripts": {
...
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/.bin/typeorm",
"migration:generate": "npm run typeorm migration:generate -- -n",
}
I replaced "typeorm" with
"scripts": {
...
"typeorm:win": "ts-node -r tsconfig-paths/register node_modules\\typeorm\\cli.js",
"migration:generate:win": "npm run typeorm:win migration:generate -- -n"
}
And then this worked like a charm npm run migration:generate:win -n <<migration name>>
Remove ts-node from your command and try again:
.\node_modules\.bin\typeorm migrations:generate -n
typeorm in your node_modules already an executable, you don't have to use ts-node, which is only means for TypeScript files.
Create a script in your package.json:
{
"scripts": {
"migrate": "typeorm migrations:generate -n"
}
}
Run it with npm:
npm run migrate
npxSince version 5.2 of npm, you can do this:
npx typeorm migrations:generate -n
yarnyarn typeorm migrations:generate -n
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With