You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在NestJS中使用MikroORM迁移时遇配置与实体发现问题

MikroORM迁移创建报错:配置文件找不到/实体未发现问题

问题描述

执行mikro-orm migration:create命令时,先后遇到两个错误:

  1. 初始报错:Error: MikroORM config file not found in ['./dist/mikro-orm.config.js', './mikro-orm.config.js']
  2. 创建mikro-orm.config文件并复制AppModule中的配置后,报错:Error: No entities discovered
  3. 尝试修改entitiesTs、entities、path、pathTs等路径(添加*、使用__dirname/process.cwd()),均未解决问题。

当前AppModule配置如下:

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MikroOrmCoreModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        entitiesTs: [`./shared/db/entities`],
        entities: [`./dist/shared/db/entities`],
        user: configService.get<string>('DB_NAME'),
        password: configService.get<string>('DB_PASSWORD'),
        dbName: configService.get<string>('DB_NAME'),
        port: configService.get<number>('DB_PORT'),
        type: 'postgresql',
        migrations: {
          tableName: 'migrations',
          path: `./dist/shared/db/migrations`,
          pathTs: `./shared/db/entities`,
          glob: '!(*.d).{js,ts}',
          transactional: true,
          allOrNothing: true,
          emit: 'ts',
        },
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

解决方案

1. 修正独立配置文件mikro-orm.config.ts

你的配置存在两处关键错误:

  • migrations.pathTs指向了实体目录,应该改为迁移目录
  • 实体路径未使用递归匹配,导致无法发现子目录下的实体

正确的配置文件示例:

import { defineConfig } from '@mikro-orm/postgresql';
import { ConfigService } from '@nestjs/config';
import * as dotenv from 'dotenv';

// CLI不会自动加载Nest的ConfigModule,手动加载环境变量
dotenv.config();
const configService = new ConfigService();

export default defineConfig({
  // 递归匹配所有实体文件
  entitiesTs: ['./shared/db/entities/**/*.ts'],
  entities: ['./dist/shared/db/entities/**/*.js'],
  // 注意:原配置中user字段误用DB_NAME,应改为数据库用户名对应的环境变量
  user: configService.get<string>('DB_USER'),
  password: configService.get<string>('DB_PASSWORD'),
  dbName: configService.get<string>('DB_NAME'),
  port: configService.get<number>('DB_PORT'),
  type: 'postgresql',
  migrations: {
    tableName: 'migrations',
    path: './dist/shared/db/migrations',
    // 迁移源文件目录改为migrations,而非entities
    pathTs: './shared/db/migrations',
    glob: '!(*.d).{js,ts}',
    transactional: true,
    allOrNothing: true,
    emit: 'ts',
  },
});

2. 确保CLI能识别配置文件

在package.json中添加MikroORM的CLI配置,明确指定配置文件路径:

{
  "mikro-orm": {
    "useTsNode": true,
    "configPaths": [
      "./mikro-orm.config.ts",
      "./dist/mikro-orm.config.js"
    ]
  }
}

或者执行命令时直接指定配置文件:

mikro-orm migration:create --config ./mikro-orm.config.ts

3. 检查实体文件的正确性

  • 所有实体类必须使用@Entity()装饰器
  • 确保实体文件后缀为.ts(开发环境),编译后生成.js文件在dist目录对应位置
  • 示例实体文件:
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';

@Entity()
export class User {
  @PrimaryKey()
  id: number;

  @Property()
  name: string;
}

4. 路径排查技巧

  • 在配置文件中添加console.log(process.cwd()),确认当前工作目录是否与预期一致
  • 实体路径必须精确匹配文件位置,若实体在子目录下,必须用**/*.ts递归查找

内容的提问来源于stack exchange,提问作者user15913196

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 19:01:17