nestjs-redis 报错的解决方案

环境

nest8 nestjs-redis

nestjs-redis 报 Please make sure that the argument ModuleRef at index [1] is available in the RedisCoreModule context.

貌似是nestjs-redis 一直没人维护,对nest8以上支持有问题,具体可以看GitHub的issues,一定自己去看

“nestjs-redis”: “^1.3.3”,

Nest can't resolve dependencies of the RedisCoreModule (Symbol(REDIS_MODULE_OPTIONS), ?). Please make sure that the argument ModuleRef at index [1] is available in the RedisCoreModule context.

Potential solutions:
- If ModuleRef is a provider, is it part of the current RedisCoreModule?
- If ModuleRef is exported from a separate @Module, is that module imported within RedisCoreModule?
  @Module({
    imports: [ /* the Module containing ModuleRef */ ]
  })

解决方案

nestjs-redis 改为 @chenjm/nestjs-redis 这是本人克隆之后自己用的,看了issues自己判断,某些人大可不必上来就喷,这里只是说明如何解决的,如果有好的,希望不吝赐教。

yarn add @chenjm/nestjs-redis

app.module.ts 模块中引入

app.module.ts

import {
    RedisModule
} from "@chenjm/nestjs-redis";

@Module({
    imports: [
        RedisModule.forRoot({
            closeClient: true,
            readyLog: true,
            config: {
                namespace: 'default',
                host: '127.0.0.1',
                port: 6380,
                password: 'redispassword'
            }
        }),
        CatsModule
    ]
})
export class AppModule {}

也可以配合 @nestjs/config 通过configService 提供配置

import { ConfigModule, ConfigService } from "@nestjs/config";
import { RedisModule } from "@chenjm/nestjs-redis";

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      cache: true,
      envFilePath: `${process.env.NODE_ENV}.env`,
      load: [configuration],
    }),
    RedisModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          closeClient: true,
          readyLog: true,
          config: configService.get("redis"),
        };
      },
    }),
  ],
})
export class AppModule {}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐