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

执行npm i后NestJS应用抛出MongoDB包未安装错误求助

NestJS应用启动报错DriverPackageNotInstalledError原因分析

错误信息

DriverPackageNotInstalledError: MongoDB package has not been found installed. Try to install it: npm install mongodb --save
    at MongoDriver.loadDependencies 
(/Users/jeanluca/dev/api/src/driver/mongodb/MongoDriver.ts:531:19)
at new MongoDriver 
(/Users/jeanluca/dev/api/src/driver/mongodb/MongoDriver.ts:237:14)
at DriverFactory.create 
(/Users/jeanluca/dev/api/src/driver/DriverFactory.ts:60:24)
at new DataSource (/Users/jeanluca/api/src/data- 
source/DataSource.ts:139:43)
at createTypeOrmDataSource 
(/Users/jeanluca/dev/api/node_modules/@nestjs/typeorm/di
...

项目配置

app.module.ts

@Module({
    imports: [
        ConfigModule.forRoot({ envFilePath, isGlobal: true }),
        TypeOrmModule.forRootAsync({ useClass: TypeOrmConfigService }),
        ApiModule,
    ],
    controllers: [AppController],
    providers: [AppService],
})
export class AppModule { }

TypeOrmConfigService配置类

@Injectable()
export class TypeOrmConfigService implements TypeOrmOptionsFactory {
  @Inject(ConfigService)
  private readonly config: ConfigService;

  public createTypeOrmOptions(): TypeOrmModuleOptions {
      return {
          type: 'mongodb',
          host: this.config.get<string>('DATABASE_HOST'), // 127.0.0.1
          port: this.config.get<number>('DATABASE_PORT'),
          database: this.config.get<string>('DATABASE_NAME'), 
          username: this.config.get<string>('DATABASE_USER'),
          password: this.config.get<string>('DATABASE_PASSWORD'),
          logging: true,
          logger: 'file',
          synchronize: false, // never use TRUE in production!
          authSource: 'admin',
          ssl: false,
          autoLoadEntities: true // Fix NX monorepo build issue with `entity` generation
};
}
}

相关依赖片段(package.json)

"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.3.1",
"@nestjs/core": "^9.0.0",
"@nestjs/jwt": "^10.0.2",
"@nestjs/passport": "^9.0.3",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/typeorm": "^9.0.1",
"mongoose": "^7.0.1",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",

可能的原因及解决方法

1. TypeORM MongoDB驱动依赖缺失

当前使用TypeORM连接MongoDB,但仅安装了mongoose包——这是Mongoose ORM的专属依赖,和TypeORM无关。TypeORM连接MongoDB需要直接依赖官方mongodb驱动包。此前项目能运行,大概率是其他依赖间接引入了这个包,执行npm i后间接依赖被移除或更新,导致包缺失。

解决: 直接安装mongodb包

npm install mongodb --save

2. 依赖版本兼容性问题

@nestjs/typeorm@9.0.1对应的TypeORM版本对mongodb包有特定版本要求,执行npm i时可能自动更新了依赖,导致版本不匹配,触发驱动加载失败。

解决:

  • 查阅@nestjs/typeorm和TypeORM官方文档,确认兼容的mongodb版本
  • 在package.json中锁定mongodb版本(例如"mongodb": "^4.17.0",需根据对应版本调整),再执行npm install

3. npm安装缓存或生产模式问题

如果执行npm i时带了--production参数,会跳过devDependencies;若mongodb此前作为dev依赖安装(虽不合理但存在可能性),会导致生产环境缺失。另外npm缓存损坏也可能导致依赖未正确安装。

解决:

  • 清理npm缓存后重新安装:
npm cache clean --force
npm install

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 18:15:01