Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

NestJS cannot start TypeORM because the data source has no usable type

Your error

MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql", "aurora-postgres", "better-sqlite3", "capacitor", "cockroachdb", ...2 more lines it can show

Fix it fast

Most common cause, 1 of 3

forRoot() is empty and the settings sit in ormconfig.js

Move the settings into the module itself, or export them from one file that both Nest and the TypeORM CLI import.

Fix
// src/app.module.ts
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: "postgres",
      host: "localhost",
      port: 5432,
      username: "app",
      password: "app",
      database: "app",
      autoLoadEntities: true,
    }),
  ],
})
export class AppModule {}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

MissingDriverError: Wrong driver: "undefined" given means TypeORM received options with no type. @nestjs/typeorm passes forRoot() options straight to new DataSource(), and TypeORM 0.3 and 1.x never read ormconfig.js for it. Put the options in forRoot() or forRootAsync(), make sure .env loads first, and on TypeORM 1.x replace type sqlite with better-sqlite3.

  • Applies to TypeORM 1.1.3, @nestjs/typeorm 12.0.3, @nestjs/core 12.6.0, @nestjs/config 12.1.1, Node.js 24.22 LTS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Look at the word inside the quotes and at how TypeOrmModule is set up. "undefined" with an empty forRoot() is cause 1; "undefined" with values taken from process.env is cause 2; any real word, such as sqlite or postgresql, is cause 3.

Run this first
grep -rn "TypeOrmModule.forRoot" src/
ls ormconfig.* .env 2>/dev/null

Then find the line you see in this list and open its fix:

  1. 1forRoot() is empty and the settings sit in ormconfig.jsapp.module.ts calls TypeOrmModule.forRoot() with no argument, and an ormconfig.js, ormconfig.json or ormconfig.env file holds the real settings
  2. 2The options read process.env before the .env file is loadedforRoot() has a line like type: process.env.DB_TYPE, and the same app works when the variable is exported in the shell first
  3. 3The type names a driver TypeORM does not haveThe quotes hold a real word, for example "sqlite" after an upgrade to TypeORM 1.x, or a spelling such as "postgresql"

What does the error look like?

  • MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql", "aurora-postgres", "better-sqlite3", "capacitor", "cockroachdb", ...
  • MissingDriverError: Wrong driver: "sqlite" given.
  • [TypeOrmModule] Unable to connect to the database. Retrying (1)...

Cause 1 of 3

forRoot() is empty and the settings sit in ormconfig.js

You seeapp.module.ts calls TypeOrmModule.forRoot() with no argument, and an ormconfig.js, ormconfig.json or ormconfig.env file holds the real settings

Check

With an empty forRoot(), @nestjs/typeorm defaults the options to {} and hands them unchanged to new DataSource(), so TypeORM sees type undefined. TypeORM 0.3.0 stopped using ormconfig outside its deprecated methods, and 1.0 removed those methods together with TYPEORM_* variables and ormconfig.env. The bare forRoot() in Nest's docs is a shorthand, not a working setup.

Check
grep -rn "forRoot()" src/app.module.ts

Fix

Move the settings into the module itself, or export them from one file that both Nest and the TypeORM CLI import. Delete ormconfig afterwards so nobody edits a file that is never read.

Fix
// src/app.module.ts
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: "postgres",
      host: "localhost",
      port: 5432,
      username: "app",
      password: "app",
      database: "app",
      autoLoadEntities: true,
    }),
  ],
})
export class AppModule {}

Verify

npm run start:dev boots without MissingDriverError, and the TypeOrmModule retry lines no longer appear in the log.

Verify
npm run start:dev

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The options read process.env before the .env file is loaded

You seeforRoot() has a line like type: process.env.DB_TYPE, and the same app works when the variable is exported in the shell first

Check

The quoted undefined also appears when the options object is built from process.env while the module file is still being evaluated. Nest's configuration docs say ConfigModule loads .env from the directory the process starts in, so a variable can be missing at that moment, or missing entirely when the app is started from another folder.

Check
grep -rn "process.env" src/app.module.ts

Fix

Switch to forRootAsync(), import ConfigModule and read values through ConfigService inside useFactory, which runs only after configuration is ready. Write the driver name as a literal; it rarely changes between environments, and a literal cannot come back undefined.

Fix
import { ConfigModule, ConfigService } from "@nestjs/config";

TypeOrmModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (config: ConfigService) => ({
    type: "postgres",
    host: config.get("DB_HOST"),
    port: +config.get("DB_PORT"),
    username: config.get("DB_USER"),
    password: config.get("DB_PASSWORD"),
    database: config.get("DB_NAME"),
    autoLoadEntities: true,
  }),
})

Verify

Start the app from a different working directory as well as from the project root; both runs connect, or both fail with a clear missing-variable message instead of a driver error.

Verify
npm run start

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The type names a driver TypeORM does not have

You seeThe quotes hold a real word, for example "sqlite" after an upgrade to TypeORM 1.x, or a spelling such as "postgresql"

Check

When the name is not on the supported list, TypeORM's driver factory throws the same MissingDriverError with your value in the quotes. TypeORM 1.0 dropped the sqlite3 package and its sqlite type, so code that ran on 0.3 with type sqlite fails right after the upgrade.

Check
npm ls typeorm better-sqlite3

Fix

Copy the type exactly from the list printed in the error. For SQLite on TypeORM 1.x, install better-sqlite3 and use that name; for PostgreSQL the value is postgres.

Fix
npm install better-sqlite3
# then in the options:
#   type: "better-sqlite3",
#   database: "data/app.sqlite",

Verify

MissingDriverError is gone. If the driver package itself were missing, TypeORM would now raise DriverPackageNotInstalledError instead, which proves the type name was accepted.

Verify
npm run start:dev

Confirmed by typeorm.io

Did this fix it?

How do I stop it coming back?

  • Keep the data source options in one exported object and import it in both AppModule and the CLI data source file, so ormconfig never creeps back.
  • Validate DB_* variables at startup with ConfigModule's validation schema, so a missing value fails with its own name instead of as a driver error.
  • Read TypeORM's release notes before a major upgrade; the 1.0 notes list removed drivers, removed env support and removed global helpers.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.