在NestJS中配置TypeOrmModule.forRoot后出现TypeError: rxjs_1.lastValueFrom is not a function错误的原因排查
Problem Description
I'm building an API with NestJS, and after adding typeorm and pg dependencies, I configured TypeOrmModule.forRoot() in app.module.ts like this:
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { CoffeesModule } from './coffees/coffees.module'; @Module({ imports: [ CoffeesModule, TypeOrmModule.forRoot({ type: 'postgres', host: 'localhost', port: 5432, username: 'postgres', password: 'xxx', database: 'postgres', autoLoadEntities: true, synchronize: true }) ], controllers: [AppController], providers: [AppService], }) export class AppModule { }
As soon as I add this TypeORM configuration, I get the error TypeError: rxjs_1.lastValueFrom is not a function. Removing the TypeOrmModule.forRoot() call makes the error disappear entirely. What could be causing this issue?
Solution
This error is almost always tied to RxJS version incompatibility or mismatched dependencies between NestJS, @nestjs/typeorm, and TypeORM itself. Here's a breakdown of the most likely causes and fixes:
1. Outdated RxJS Version
The lastValueFrom method was introduced in RxJS 7.0.0. If your project is running RxJS 6 or older, TypeORM (which relies on this method for async database operations) will throw this exact error.
Fix:
- First, check your current RxJS version:
npm list rxjs # For Yarn users: yarn list rxjs - If the version is below 7, upgrade to the latest compatible release:
npm install rxjs@^7.8.1 # For Yarn users: yarn add rxjs@^7.8.1
2. Mismatched @nestjs/typeorm and TypeORM Versions
Different versions of @nestjs/typeorm require specific TypeORM versions to work correctly. For example:
@nestjs/typeorm@9.xpairs with TypeORM0.3.x@nestjs/typeorm@8.xpairs with TypeORM0.2.x
If you've installed a TypeORM version that's incompatible with your @nestjs/typeorm setup, it can pull in conflicting RxJS dependencies or use unsupported methods.
Fix:
- Align your versions to match the official compatibility guidelines. For example, if you're using
@nestjs/typeorm@9, update TypeORM to the correct range:npm install typeorm@^0.3.17 # For Yarn users: yarn add typeorm@^0.3.17
3. Corrupted Node Modules or Lock File
Sometimes, cached dependencies in node_modules or a mismatched lock file can create hidden version conflicts that trigger this error.
Fix:
- Delete your dependency folder and lock file, then reinstall everything fresh:
# For npm: rm -rf node_modules package-lock.json npm install # For Yarn: rm -rf node_modules yarn.lock yarn install
内容的提问来源于stack exchange,提问作者npkp

