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

在NestJS中配置TypeOrmModule.forRoot后出现TypeError: rxjs_1.lastValueFrom is not a function错误的原因排查

TypeError: rxjs_1.lastValueFrom is not a function when using TypeOrmModule.forRoot() in NestJS

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.x pairs with TypeORM 0.3.x
  • @nestjs/typeorm@8.x pairs with TypeORM 0.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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 21:17:34