typeorm: TypeORM does not work with Typscript and NestJS

Issue description

Can’t generate migrations

Expected Behavior

I expect migration:generate to create a migration file.

Actual Behavior

I run this command: pnpm run typeorm migration:generate -- ./src/migrations/ -d ./src/books.datasource.ts

Which is adapted from the documentation of how to get the typorm CLI to run in Typescript environments: https://typeorm.io/using-cli#if-entities-files-are-in-typescript.

That seems to run:

> typeorm-ts-node-commonjs "migration:generate" "--" "./src/migrations/" "-d" "./src/books.datasource.ts"

And then comes back with:

Not enough non-option arguments: got 0, need at least 1
 ELIFECYCLE  Command failed with exit code 1.

Steps to reproduce

See above for the exact commands

My Environment

Dependency Version
Operating System macOS
Node.js version 18.17.1
Typescript version 4.9.5
TypeORM version 0.3.17

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don’t know how to start. I would need guidance.

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Comments: 15

Most upvoted comments

TypeORM definitely works with Typescript and NestJs, I am currently using it for my project and have used it with previous projects.

You haven’t provided your typeorm datasource so it’s hard to even begin helping you debug your DataSourceOptions.

Here is an example of my data-source.ts file in db/

import { DataSource, DataSourceOptions } from 'typeorm';

export const dataSourceOptions: DataSourceOptions = {
  type: 'postgres',
  host: process.env.POSTGRES_HOST || 'localhost',
  port: Number(process.env.POSTGRES_PORT) || 5432,
  username: process.env.POSTGRES_USER
  password: process.env.POSTGRES_PASSWORD
  database: process.env.POSTGRES_DB
  entities: ['dist/libs/common/src/entities/*.entity{.ts,.js}'],
  migrations: ['dist/db/migrations/*{.ts,.js}'],
  migrationsTableName: 'migrations',
  // logging: true, // Enable if require to see DB queries/logs
};

// Used by TypeORM CLI for migrations
const dataSource = new DataSource(dataSourceOptions);

export default dataSource;

Here is an example of my migration scripts in package.json

  "build:migrations": "npx tsc -p db/tsconfig.app.json",
  "typeorm": "npx typeorm-ts-node-commonjs -d dist/db/data-source.js",
  "migration:generate": "npm run typeorm migration:generate",
  "migration:create": "npm run typeorm -- migration:create",
  "migration:run": "npm run typeorm -- migration:run",

Here is an example of how I use them:

  1. Build the migration dependencies: npm run build:migrations
  2. Generating migrations based on a schema: npm run migration:generate -- db/migrations/<name of migration>