mikro-orm

MikroORM CLI Error: No database specified, please fill in `dbName` or `clientUrl` option


Trying to get the MikroORM CLI running, but no luck.

I have NX monorepo with MikroORM being a library.

I have tried with .env variables and with package.json but getting different errors for them.

Using package.json

This is the package.json

{
   "name":"@my-app",
   "version":"1.0.0",
   "license":"MIT",
   "scripts":{},
   "private":true,
   "dependencies":{},
   "devDependencies":{},
   "mikro-orm":{
      "useTsNode":true,
      "tsConfigPath":"./libs/database/tsconfig.orm.json",
      "configPaths":[
         "./libs/database/src/lib/mikro-orm.config.ts",
         "./dist/libs/database/src/lib/mikro-orm.config.js"
      ]
   }
}

This is the MikroORM config file

import { ConfigService } from '@nestjs/config'
import { MikroOrmModuleOptions } from '@mikro-orm/nestjs'
import { MySqlDriver, ReflectMetadataProvider } from '@mikro-orm/mysql'
import { SeedManager } from '@mikro-orm/seeder'
import { Migrator } from '@mikro-orm/migrations'

import { SoftDeletableHandlerSubscriber } from '@my-app/core'

const mikroOrmConfig = {
    user: process.env['DB_USERNAME'],
    host: process.env['DB_HOST'],
    password: process.env['DB_PASSWORD'],
    dbName: process.env['DB_NAME'],
    port: parseInt(process.env['DB_PORT'] as string),

    driver: MySqlDriver,
    autoLoadEntities: true,
    entities: ['dist/**/entities/*.js'],
    entitiesTs: ['src/**/entities/*.ts'],
    metadataProvider: ReflectMetadataProvider,
    extensions: [SoftDeletableHandlerSubscriber, SeedManager, Migrator],
    registerRequestContext: true,
    allowGlobalContext: process.env['NODE_ENV'] === 'test',
    migrations: {
        tableName: 'migration',
        path: './migrations',
        snapshot: true
    },
    seeder: {
        path: './seeders',
        pathTs: './seeders',
        defaultSeeder: 'DatabaseSeeder',
        glob: '!(*.d).{js,ts}',
        emit: 'ts',
        fileName: (className: string) => className
    }
} as MikroOrmModuleOptions

export default mikroOrmConfig

And this is the tsconfig.orm.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "moduleResolution": "NodeNext"
  }
}

This is the tsconfig.json which is used for extending

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "module": "commonjs",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  },
  "files": [],
  "include": [],
  "references": [
    {
      "path": "./tsconfig.lib.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ]
}

When running npx mikro-orm debug I get the following output

Current MikroORM CLI configuration
 - dependencies:
   - mikro-orm 6.2.9
   - node 18.17.0
   - typescript 5.3.3
 - package.json found
 - ts-node enabled
 - searched config paths:
   - C:/Work/my-company/my-app/code/my-app-backend/libs/database/src/lib/mikro-orm.config.ts (found)
   - C:/Work/my-company/my-app/code/my-app-backend/dist/libs/database/src/lib/mikro-orm.config.js (found)     
   - C:/Work/my-company/my-app/code/my-app-backend/src/mikro-orm.config.ts (not found)
   - C:/Work/my-company/my-app/code/my-app-backend/mikro-orm.config.ts (not found)
   - C:/Work/my-company/my-app/code/my-app-backend/dist/mikro-orm.config.js (not found)
   - C:/Work/my-company/my-app/code/my-app-backend/mikro-orm.config.js (not found)
- configuration not found (No database specified, please fill in `dbName` or `clientUrl` option)

Using .env variables

These are my .env variables

# MIKRO ORM
MIKRO_ORM_CLI_USE_TS_NODE=true
MIKRO_ORM_CLI_ALWAYS_ALLOW_TS=true
MIKRO_ORM_CLI_CONFIG="./libs/database/src/lib/mikro-orm.config.ts"
MIKRO_ORM_CLI_TS_CONFIG_PATH="./libs/database/tsconfig.json"

and this is the output I get when running the same command

Current MikroORM CLI configuration
 - dependencies:
   - mikro-orm 6.2.9
   - node 18.17.0
   - typescript 5.3.3
 - package.json found
 - ts-node enabled
 - searched config paths:
   - C:/Work/my-company/my-app/code/my-app-backend/libs/database/src/lib/mikro-orm.config.ts (found)
   - C:/Work/my-company/my-app/code/my-app-backend/src/mikro-orm.config.ts (not found)
   - C:/Work/my-company/my-app/code/my-app-backend/mikro-orm.config.ts (not found)
   - C:/Work/my-company/my-app/code/my-app-backend/dist/mikro-orm.config.js (not found)
   - C:/Work/my-company/my-app/code/my-app-backend/mikro-orm.config.js (not found)
- configuration not found (Cannot use import statement outside a module)

The environment variable exists and app is running/building fine.

EDIT 1

When doing only the ENV variables approach

MIKRO_ORM_CLI_USE_TS_NODE=true
MIKRO_ORM_CLI_CONFIG="./libs/database/src/lib/mikro-orm.config.ts"
MIKRO_ORM_CLI_TS_CONFIG_PATH="./libs/database/tsconfig.json"

As you said I shouldn't be needing tsconfig.orm.json so I am using the tsconfig that I used for extending the tsconfig.orm.ts and I removed the MikroORM section from the package.json I get the following error:

Current MikroORM CLI configuration
 - dependencies:
   - mikro-orm 6.2.9
   - node 18.17.0
   - typescript 5.3.3
 - package.json found
 - ts-node enabled
 - searched config paths:
   - C:/Work/my-company/my-app/code/my-app-backend/dist/mikro-orm.config.js (not found)
   - C:/Work/my-company/my-app/code/my-app-backend/mikro-orm.config.js (not found)
- configuration not found (MikroORM config file not found in ['./dist/mikro-orm.config.js', './mikro-orm.config.js'])
  1. ts-node has been installed (I didn't include the dependencies so it wouldn't get this long) but it's "ts-node": "^10.9.1"

With the package.json way, commenting out the env variables totally and while still referencing the ./libs/database/tsconfig.orm.json and after adding import 'dotenv/config' I now get it to run. But if I change the tsConfigPath to the previously mentioned tsconfig.json I get the following error:

 Current MikroORM CLI configuration
     - dependencies:
       - mikro-orm 6.2.9
       - node 18.17.0
       - typescript 5.3.3
     - package.json found
     - ts-node enabled
     - searched config paths:
       - C:/Work/my-company/my-app/code/my-app-backend/libs/database/src/lib/mikro-orm.config.ts (found)
       - C:/Work/my-company/my-app/code/my-app-backend/dist/libs/database/src/lib/mikro-orm.config.js (found)     
       - C:/Work/my-company/my-app/code/my-app-backend/src/mikro-orm.config.ts (not found)
       - C:/Work/my-company/my-app/code/my-app-backend/mikro-orm.config.ts (not found)
       - C:/Work/my-company/my-app/code/my-app-backend/dist/mikro-orm.config.js (not found)
       - C:/Work/my-company/my-app/code/my-app-backend/mikro-orm.config.js (not found)
    - configuration not found (⨯ Unable to compile TypeScript:
    error TS5109: Option 'moduleResolution' must be set to '
    NodeNext' (or left unspecified) when option 'module' is set to 'NodeNext'.
    )

Solution

  • A few random notes, if you want more help with this, please get in touch on discord and provide a complete repository instead of just snippets.

    1. You do not want MIKRO_ORM_CLI_ALWAYS_ALLOW_TS, that means you need to bring TS support yourself, e.g. by executing via bun -b (which has its own pitfalls, since bun is far away from being actually node compatible).

    2. You need to have ts-node installed if you want the ORM to use it, I don't see it in your dependencies. This is likely the reason why you see Cannot use import statement outside a module since that means you are trying to read TS file without ts-node registered.

    3. You should not need the tsconfig.orm.json file, the ORM will automatically override the moduleResolution to NodeNext actually for the purpose of registering TS support. Also in your second approach, you are overriding the path to it via MIKRO_ORM_CLI_TS_CONFIG_PATH env var which points to something else, so in fact its completely ignored there.

    4. When you were getting No database specified, please fill in dbNameorclientUrl option, it most likely means your env vars were empty. My guess is you forgot to call import 'dotenv/config' in your config - the ORM will no longer do that for you since v6, it will still read it automatically, but will only respect its own env vars (those prefixed with MIKRO_ORM_) and won't make them part of process.env at all.