When I run the Nestjs server, I get the following error. I am using the NestJS BullModule.
Error: Nest can't resolve dependencies of the
BullExplorer (?, DiscoveryService, BullMetadataAccessor, MetadataScanner) Please make sure that the argument ModuleRef at index [0] is available in the BullModule context.
Potential solutions:
If ModuleRef is a provider, is it part of the current BullModule?
If ModuleRef is exported from a separate #Module, is that module imported within BullModule?
#Module({ imports: [ /* the Module containing ModuleRef */ ] })
Here is the Bull module configuration:
BullModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
const redisConfig = configService.get<RedisConfig>("redis");
return {
redis: {
host: redisConfig.host,
port: redisConfig.port,
},
defaultJobOptions: {
timeout: 30000,
removeOnComplete: true,
removeOnFail: true,
attempts: 3,
},
};
},
inject: [ConfigService],
}),
BullModule.registerQueue({
name: MAIL_QUEUE,
}),
Turns out the problem is known in NestJS community. There is a whole thread on their discord here and PR to the documentation, mentioning how to fix the problem, waiting for merging here. Long story short, if you're working in monorepo setup with yarn workspaces, you don't want to hoist your #nestjs dependencies. Configure this in your root level package.json like this:
{
"workspaces": {
"packages": [],
"nohoist": [
"**/#nestjs/**"
]
}
}
The problem has to do with how yarn workspaces work... my monorepo did not work properly when the Nestjs Bull module was installed in it using yarn v. 1.18.0.
My Original Answer:
Upgraded the yarn from 1.18.0 to 1.22.15
Edited Answer:
After deleting the node_modules folders, and doing the fresh install using yarn 1.22.15.. I bumped into the same error message again.
Finally, I decided to use pnpm workspaces and the new setup worked like a charm.
Hope someone may find it useful.
Related
I am migrating some jest tests over to vitest and some of my tests are failing due to an import issue of an external package dependency in node_modules. Specifically: #package/dependency seems to be an ES Module but shipped in a CommonJS package.
vitest suggests this change to my config:
export default {
test: {
deps: {
inline: [
"#package"
]
}
}
}
Unfortunately, this fix does not work. Previously I resolved this issue with moduleNameMapper in jest where "#package/dependency": "#package/dependency/js" mapped to a valid import. I tried setting alias in both test.alias and resolve.alias, but neither works.
I am using Typescript in this project, and the rest of my test config looks like this:
test: {
globals: true,
environment: "jsdom",
setupFiles: "./src/setupTests.js",
}
// setupTests.js
import {configure} from 'enzyme/build';
import Adapter from 'enzyme-adapter-react-16/build';
configure({ adapter: new Adapter() });
What can I do to get around this? Thanks.
I have a weird behaviour. Nest is complaining about a missing dependency in a service, but only if that service is Injected by multiple other services
cleaning.module.ts
#Module({
imports: [
//Just a few repos
],
providers: [
ServicesService,
...
AreasService,
RoomsService,
...
],
controllers: [
...
],
exports: [
ServicesService,
...
AreasService,
RoomsService,
...
]})
services.module.ts, areas.module.ts and rooms.module.ts are basically the same
#Module({
imports: [CleaningModule],
providers: [],
controllers: [],
exports: []
})
rooms.service.ts
constructor(#InjectRepository(Room) private readonly repo: Repository<Room>, private areasService: AreasService) { }
services.service.ts
constructor(#InjectRepository(Service) private readonly repo: Repository<Service>, ... private roomsService: RoomsService) { }
With this, I get the following error:
ERROR [ExceptionHandler] Nest can't resolve dependencies of the RoomsService (RoomRepository, ?). Please make sure that the argument dependency at index [1] is available in the CleaningModule context.
So, it's missing AreasService somehow. Which is clearly provided by the CleaningModule.
But the weird thing is, if I remove the injection of the RoomsService in the ServicesService constructor, then everything works just fine. And I don't have the slightest idea why this is the case...
Based on the error it seems like you have a circular file import going on from your roo.service.ts to your area.service.ts. This could be several files deep like room.service.ts imports area.service.ts that another that imports another that imports another that finally imports room.service.ts again or it could be the use of barrel files. If this truly is a circular dependency (room uses area which uses room) then you need to use #Inject(forwardRef(() => OtherService)). Otherwise, you need to figure out a way to avoid the circular import
I've followed the instruction from nestjs-redis documentation to register RedisModule but it gives me the following error message. I am new to Nest.js and have no idea how to fix this error.
If you know the better way to use Redis with nestjs, please suggest me.
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 */ ] })
my app.module
#Module({
imports: [
RedisModule.register({
host: `${host}`,
port: 6379,
}),
],
})
export class AppModule {}
I am building a nestjs app where I want to create a rabbitmq
#Module({
imports: [
ClientsModule.register([
{
name: 'rabbitmq',
transport: Transport.RMQ,
options: {
urls: [
'amqp://guest:guest#rabbitmq',
],
queue: 'my_queue',
},
},
]),
],
controllers: [],
providers: [RabbitMQService],
exports: [RabbitMQService],
})
And service:
#Injectable()
export class RabbitMQService {
constructor(
#Inject('rabbitmq') private client: ClientProxy
) {}
}
The error I am getting is: Nest can't resolve dependencies of the RabbitMQService (?). Please make sure that the argument rabbitmq at index [0] is available in the RabbitMQService context.
As much I am aware, this should work, but nope. Could anyone help?
From the error, it looks like somewhere in your application you have RabbitMQService in an imports array where #Module() classes are supposed to go. Make sure that you keep providers and other #Injectables() to the providers array and keep #Module() and other DynamicModules to the imports array. Common error docs
I have an Nest.js application. I wanted to add MailerModule to my app using following link -> https://npm.taobao.org/package/#nest-modules/mailer
However, I just did the following steps:
First, npm install --save #nest-modules/mailer
Second I add app module mail config but it is giving an error here is my app.module.ts:
import { Module } from '#nestjs/common';
import { HandlebarsAdapter, MailerModule } from '#nest-modules/mailer';
#Module({
imports: [
MailerModule.forRootAsync({
useFactory: () => ({
transport: 'smtps://user#domain.com:pass#smtp.domain.com',
defaults: {
from:'"nest-modules" <modules#nestjs.com>',
},
template: {
dir: __dirname + '/templates',
adapter: new HandlebarsAdapter(), // or new PugAdapter()
options: {
strict: true,
},
},
}),
}),],
})
export class ApplicationModule {}
Now I can't compile because it is saying that :
TS2345: Argument of type '{ imports: DynamicModule[]; }' is not assignable to parameter of type 'ModuleMetadata'. Types of property 'imports' are incompatible. Type 'DynamicModule[]' is not assignable to type '(Type | DynamicModule | Promise | ForwardReference)[]'. Type 'DynamicModule' is not assignable to type 'Type | DynamicModule | Promise | ForwardReference'. Type 'DynamicModule' is not assignable to type 'ForwardReference'. Property 'forwardRef' is missing in type 'DynamicModule'.
Perhaps check what version of nest you're using, this issue may be resolved in versions of nestjs 6+:
https://github.com/nestjs/nest/issues/669
I had a similar problem. I started to make a new project based on the old one. After installing the modules, this error appeared. The problem lay in private modules. They did not install without package-lock.json.