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

使用cache-manager@6.1.3时出现cacheManager.caching is not a function错误求助

解决NestJS中使用cache-manager@6.1.3时的cacheManager.caching is not a function错误

问题原因

cache-manager v6对核心API做了重构,移除了旧版本里的caching方法,但你当前用的NestJS内置CacheModule(来自@nestjs/common)是基于旧版cache-manager的API实现的,两者不兼容,所以启动时触发报错。

解决方案

提供两种可行方案,按需选择:

方案1:降级cache-manager到兼容版本

直接把cache-manager降级到NestJS内置CacheModule支持的版本(比如v4.x系列,适配绝大多数NestJS稳定版本):

npm install cache-manager@^4.1.0

降级后不用改任何代码,原有的CacheModule配置和控制器装饰器可以正常运行。

方案2:切换到@nestjs/cache-manager适配新版cache-manager

如果想保留cache-manager@6.1.3,就用NestJS官方推出的独立缓存模块包@nestjs/cache-manager,它专门适配新版cache-manager:

  1. 安装依赖:
npm install @nestjs/cache-manager cache-manager@^6.1.3
  1. 修改模块导入:把CacheModule的导入源从@nestjs/common换成@nestjs/cache-manager
import { Module } from '@nestjs/common';
import { CacheModule } from '@nestjs/cache-manager';
import { AreaController } from './area.controller';
import { AreaService } from './area.service';
import { PrismaService } from '../prisma/prisma.service';

@Module({
  imports: [CacheModule.register({ ttl: 604800000 })],
  controllers: [AreaController],
  providers: [AreaService, PrismaService],
})
export class AreaModule {}
  1. 修改控制器的装饰器和拦截器导入:同样切换到@nestjs/cache-manager
import { Get, UseInterceptors } from '@nestjs/common';
import { CacheTTL, CacheInterceptor } from '@nestjs/cache-manager';
import { AreaService } from './area.service';

export class AreaController {
  constructor(private readonly areaService: AreaService) {}

  @Get()
  @CacheTTL(604800000)
  @UseInterceptors(CacheInterceptor)
  async getAll(): Promise<city[]> {
    return await this.areaService.getAll();
  }
}

内容的提问来源于stack exchange,提问作者邱杰克

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 03:22:38