inVoKer
inVoKer

Reputation: 59

Mikro-Orm Error: No driver specified when Run Mikro Orm Migration Commands against Compiled Js Files

I know there is a similar issue here but it seems not solving my issue. I also opened a github discussion here

Summary

When using MikroORM with a configuration file bundled by Webpack, the migration commands MIKRO_ORM_CLI_USE_TS_NODE=false npx mikro-orm debug/migration-check against compiled js files fail with the following error:

Error: No driver specified, please fill in the `driver` option or use `defineConfig` helper (to define your ORM config) or `MikroORM` class (to call the `init` method) exported from the driver package (e.g. `import { defineConfig } from '@mikro-orm/mysql'; export defineConfig({ ... })`).
    at Configuration.validateOptions (tryAndError/node_modules/@mikro-orm/core/utils/Configuration.js:374:19)
    at new Configuration (node_modules/@mikro-orm/core/utils/Configuration.js:152:18)
    at ConfigurationLoader.getConfiguration (tryAndError/node_modules/@mikro-orm/core/utils/ConfigurationLoader.js:103:16)
    at async CLIHelper.getORM (tryAndError/node_modules/@mikro-orm/cli/CLIHelper.js:26:25)
    at async MigrationCommandFactory.handleMigrationCommand (tryAndError/node_modules/@mikro-orm/cli/commands/MigrationCommandFactory.js:85:21)

The issue does not occur in development mode (run mikro-orm commands against ts files), where the configuration resolves correctly. Upon inspecting the configuration options by modifying the files inside node_modules/@mikro-orm/core/utils/Configuration.js using console.log(options), the options differ between development and production:

Development: Options resolve as expected.

Production: Options appear to default to the raw constructor without the expected configuration values.

Minimum Reproducible Example

# mikro-orm.config.ts

import type { AbstractSqlDriver } from '@mikro-orm/mysql';
import { MySqlDriver } from '@mikro-orm/mysql';
import type { MikroOrmModuleOptions } from '@mikro-orm/nestjs';
import { User } from './src/app/entity';
import { CLIHelper } from '@mikro-orm/cli';
import { Migrator } from '@mikro-orm/migrations';

console.log('To avoid tree shaking...', CLIHelper.getNodeVersion());
const MIKRO_ORM_CONFIG: MikroOrmModuleOptions<AbstractSqlDriver> = {
  entities: [User],
  name: 'test',
  driver: MySqlDriver,
  port: 3306,
  forceUtcTimezone: true,
  dbName: process.env.name,
  host: process.env.host,
  user: process.env.user,
  password: process.env.password,
  discovery: { disableDynamicFileAccess: true },
  migrations: {
    tableName: 'migrations',
    path: './migrations',
  },
  allowGlobalContext: process.env.MIKRO_ORM_ALLOW_GLOBAL_CONTEXT === 'true',
  extensions: [Migrator]
};

export default MIKRO_ORM_CONFIG;
# entity/user.ts

import { Entity, PrimaryKey } from '@mikro-orm/core';
import { Exclude, Expose } from 'class-transformer';

@Entity({ tableName: 'users' })
@Exclude()
export class User {
  @PrimaryKey()
  @Expose()
  id!: number;
}

Steps to Reproduce

  1. clone this sample repo here (Or c&p the minimum reproducible example and set up everything else to build as recommended by the comment)
  2. install dependencies npm install
  3. build project nx run tryAndError:build
  4. cd into dist/apps/tryAndError Run MIKRO_ORM_CLI_USE_TS_NODE=false npx mikro-orm migration:check Then you will see the error.

Run the migration commands agains the ts files in the root level, no error occurs.

Upvotes: -1

Views: 32

Answers (0)

Related Questions