Typeorm - Fix for MissingDriverError: Wrong driver: undefined in NestJS application
In the NestJS application, Configured the Mysql database with the TypeOrm framework and got an issue while starting the Nestjs app using the npm run start command.
[Nest] 12964 - 23/10/2022, 12:26:06 pm ERROR [TypeOrmModule] Unable to connect to the database. Retrying (5)…
MissingDriverError: Wrong driver: “undefined” given. Supported drivers are: “aurora-mysql”, “aurora-postgres”, “better-sqlite3”, “capacitor”, “cockroachdb”, “cordova”, “expo”, “mariadb”, “mongodb”, “mssql”, “mysql”, “nativescript”, “oracle”, “postgres”, “react-native”, “sap”, “sqlite”, “sqljs”, “spanner”.
at DriverFactory.create (A:\work\nestapp\src\driver\DriverFactory.ts:72:23)
at new DataSource (A:\work\nestapp\src\data-source\DataSource.ts:139:43)
at createTypeormDataSource (A:\work\nestapp\node_modules@nestjs\typeorm\dist\typeorm-core.module.js:172:23)
at Function.
In the NestJS application, During startup, the Application is connected to the configured database, then database configuration is not properly applied to typeorm since typeorm is an object-relational mapping for a database.
This issue is with any database such as MySQL, Oracle, or MongoDB database.
This is an issue with an incorrect database connection configuration passed to typeorm.
typeorm MissingDriverError: Wrong driver: “undefined” given
Please follow the below steps to fix the issue in the NestJS application with TypeORM integration.
- First check typeorm module configuration is passed to type
Replace the below empty forRoot()
@Module({
imports: [TypeOrmModule.forRoot()],
controllers: [],
providers: [],
})
export class AppModule {}
with
@Module({
imports: [
TypeOrmModule.forRoot({
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "",
database: "test",
entities: ["dist/**/*.entity{.ts,.js}"],
synchronize: true,
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
This error also comes when you created a configuration file that is not exported correctly. Please follow the below steps if the application contains a configuration file
if you created a configuration file orm.config.ts file, that contains the database configuration file.
In this file, please make sure that you use export default
to be used this as an object in AppModule.
import { DataSourceOptions } from "typeorm";
const config: DataSourceOptions = {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "",
database: "test",
entities: ["dist/**/*.entity{.ts,.js}"],
synchronize: true,
};
export default config;
Then, In Application Module, import orm.config file as an object and configure to TypeOrmModule.forRoot(ormconfig)
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { EmployeeModule } from "./employee/employee.module";
import ormconfig from "./config/orm.config";
@Module({
imports: [TypeOrmModule.forRoot(ormconfig)],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}