NestJS Typorm Cannot Generate Migrations

List item

I am creating my first NestJs app using Typeorm

I cannot generate a migration to automatically create a migration with up() and down() methods auto generated

Here is my package.json

scripts:{
...
 "typeorm": "typeorm-ts-node-commonjs",
 "schema:sync": "npx typeorm-ts-node-commonjs schema:sync",
 "typeorm:cache": "npx typeorm-ts-node-commonjs cache:clear",
 "schema:drop": "npx typeorm-ts-node-commonjs -d ./src/data/data-source.ts",
 "migration:create": "typeorm migration:create ./src/data/migrations/schema-update",
 "migration:generate": "npx typeorm-ts-node-commonjs migration:generate ./src/data/migrations/schema-update -d ./src/database/data-source.ts",
 "migration:show": "npx typeorm-ts-node-commonjs migration:show -d ./src/data/data-source.ts",
 "migration:run": "npx typeorm-ts-node-commonjs migration:run -d  ./src/data/data-source.ts",
 "migration:revert": "npx typeorm-ts-node-commonjs migration:revert -d ./src/data/data-source.ts",
...},
...
 "dependencies": {
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.2.3",
    "@nestjs/core": "^10.0.0",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/typeorm": "^10.0.2",
    "mysql2": "^3.11.3",
    "reflect-metadata": "^0.2.2",
    "rxjs": "^7.8.1",
    "typeorm": "^0.3.20"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/supertest": "^6.0.0",
    "@typescript-eslint/eslint-plugin": "^8.0.0",
    "@typescript-eslint/parser": "^8.0.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^7.0.0",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
...

my data-source.ts located at src/data/data-source.ts

import { DataSource } from 'typeorm';
import * as dotenv from 'dotenv';

dotenv.config();

const AppDataSource = new DataSource({
  type: 'mysql',
  host: 'localhost',
  port: Number(process.env.DB_PORT),
  username: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_DATABASE,
  entities: ['src/data/entities/*.entity{.ts}'],
  migrations: ['src/data/migrations/*{.ts,.js}'],
  synchronize: false,
  dropSchema: false,
  logging: true,
  logger: 'file',
  subscribers: ['src/data/subscribers/**/*.ts'],
  migrationsTableName: 'migration_table',
});

export default AppDataSource;

my app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import AppDataSource from './data/data-source';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    TypeOrmModule.forRoot(AppDataSource.options),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get('DB_HOST'),
        port: configService.get('DB_PORT'),
        username: configService.get('DB_USERNAME'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_DATABASE'),
        entities: ['dist/**/*.entity{.ts,.js}'],
        synchronize: false,
        autoLoadEntities: true,
        migrations: [__dirname + '/data/migrations/**/*{.ts,.js}'],
        seeds: [__dirname + '/data/seeds/**/*{.ts,.js}'],
        factories: [__dirname + '/factories/**/*{.ts,.js}'],
        cli: {
          migrationsDir: __dirname + '/data/migrations/',
        },
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

I have import "reflect-metadata" in my main.js

I run in my terminal npm run migration:generate

I get the below error


> [email protected] migration:generate
> npx typeorm-ts-node-commonjs migration:generate ./src/data/migrations/schema-update -d ./src/databasedata/data-source.ts       
      

Error during migration generation:                
Error: Unable to open file: "D:projectbackendsrcdatabasedata-source.ts". Cannot find module 'D:projectbackendsrcdatabasedata-source.ts'
Require stack:
- D:projectnode_modulestypeormutilImportUtils.js
- D:projectnode_modulestypeormcommandsCommandUtils.js
- D:projectnode_modulestypeormcommandsSchemaSyncCommand.js    
- D:projectnode_modulestypeormcli.js  
- D:projectnode_modulestypeormcli-ts-node-commonjs.js
    at Function.loadDataSource (D:projectnode_modulessrccommandsCommandUtils.ts:22:19) 
    at async Object.handler (D:projectnode_modulessrccommandsMigrationGenerateCommand.ts:78:26)
npm error Lifecycle script `migration:generate` failed with error:
npm error code 1
s /c npx typeorm-ts-node-commonjs migration:generate ./src/data/migrations/schema-update -d ./src/database/data-source.ts

N.B. The project workspace has the structure

project:
- frontend
- backend

1

Thanks to @mahig, there was incorrect path in package.json
"migration:generate": "npx typeorm-ts-node-commonjs migration:generate ./src/data/migrations/schema-update -d ./src/database/data-source.ts",
had to replace database with data (well, I copied it as is from the tutorial)

but got another error saying

No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command
npm error Lifecycle script `migration:generate` failed with error:

and the solution was using dist in the entities of the data-source.ts. (However, I do not know why in development I shall read the migration of dist

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật