Dulara Malindu
Dulara Malindu

Reputation: 1637

Typeorm Migration:generate failure Not enough non-option arguments: got 0, need at least 1

I tried this command in different possible ways but the basic structure of my command was.

 yarn typeorm migration:generate -n=consent-record -d=\"./src/db/CliDataSource.ts\"

this is my typeorm command in the package.json for yarn berry

"typeorm": "ts-node -P ./tsconfig.typeorm.json $(yarn bin typeorm) -d ./src/db/CliDataSource.ts",

I also tried installing typeorm locally as an npm. and also tried with npx. but they all give the following error. "Not enough non-option arguments: got 0, need at least 1" this error clearly doesn't mention what is missing.

my CliDataSource goes like this.

export const CliDataSource = new DataSource({
  type: 'postgres',
  host: 'localhost',
  port: 5436,
  username: '****',
  password: '******',
  database: 'consent',
  synchronize: false,
  logging: false,
  entities,
  migrations,
  migrationsRun: true,
  subscribers: [],
});

enter image description here

I am using typeorm "^0.3.6"

Upvotes: 15

Views: 18482

Answers (4)

Paul Munyao
Paul Munyao

Reputation: 195

If the problem appears in an angular application the problem is that you are running the wrong command.Open the package.json file and check the commands. The problem was that I was running

ng run

instead of

ng serve

Upvotes: 0

Ashok Dey
Ashok Dey

Reputation: 909

I had a similar issue but solved it initially using a util file like this

// contents of migration.ts

import { exec } from 'child_process';

const command = `npm run typeorm migration:create ./src/migrations/${process.argv[process.argv.length - 1]}`;

(() => exec(command, (error, stdout, stderr) => {
  if (error !== null) {
    console.error(stderr);
  }
  console.log(stdout);
}))();

In the package.json:

"migration:create": "ts-node migration.ts"

And to use, type the following:

npm run migration:create unique-key-username

But here's how it should be done after the latest changes in TypeORM:


// new syntax for TypeORM ormconfig.ts

const { DataSource } = require("typeorm");

require('dotenv').config();
for (const envName of Object.keys(process.env)) {
  process.env[envName] = process.env[envName].replace(/\\n/g, '\n');
}

const connectionSource = new DataSource({
  type: 'mysql',
  host: process.env.DB_HOST,
  port: +process.env.DB_PORT,
  username: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_DATABASE,
  entities: [__dirname + '/entities/**/*.{js,ts}'],
  migrations: [__dirname + '/dist/src/migrations/*.js'],
});

module.exports = {
  connectionSource,
}

// package.json

"typeorm": "ts-node node_modules/typeorm/cli.js",
"migration:create": "ts-node migration.ts  -d ./ormconfig.ts",
"migration:run": "typeorm migration:run -d ./ormconfig.ts",
"migration:revert": "typeorm migration:revert  -d ./ormconfig.ts",

Upvotes: 2

Shanmukh
Shanmukh

Reputation: 11

Had the same issue, the workaround I found for now is to use the compiled js file

For example, this will work

npx typeorm migration:generate Init -d dist/db/CliDataSource.js

As the CLI command fails to import the .ts file for some reason, probably because of the file URL it tries to generate, also for some reason it will only import a ts file if the closest package.json file is set to a type => module

File URL ->

typeorm/src/db/CliDataSource.ts


: pathToFileURL(filePath).toString(), 
package.json module type check ->

typeorm/src/db/CliDataSource.ts/

 const isModule = (packageJson as any)?.type === "module" 

This command below does not work

npx typeorm migration:generate Init -d ./src/db/CliDataSource.ts

Upvotes: 1

Dulara Malindu
Dulara Malindu

Reputation: 1637

Latest updates to the typeorm has removed -n flag which we used to rename migrations. how it works now is that we need to provide the migration file path. that will store the migration in that specified file. so the updated operations were

my typeorm alias inside package.json.

"typeorm": "ts-node -P ./tsconfig.typeorm.json $(yarn bin typeorm) -d ./src/db/CliDataSource.ts",

CLI Command

 yarn typeorm migration:generate ./src/db/migrations/consent-record

The official documentation seems outdated. hope it will be updated soon.

Special thanks to Jacob Copini @woov

Upvotes: 23

Related Questions