When trying to use another global
module's service from one global
module, I get an error thrown by NestJs at launch time. Nest complains that it cannot resolve the dependency.
The error thrown by Nest:
[ExceptionHandler] Nest can't resolve dependencies of the LoggerProvider (?). Please make sure that the argument dependency at index [0] is available in the LoggerModule context.
Potential solutions:
- If dependency is a provider, is it part of the current LoggerModule?
- If dependency is exported from a separate @Module, is that module imported within LoggerModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
config.module.ts
:import { Module, Global } from '@nestjs/common';
import { ConfigService } from './config.service';
@Global()
@Module({
providers: [ConfigService],
exports: [ConfigService],
})
export class ConfigModule {}
logger.module.ts
:import { Module, Global } from '@nestjs/common';
import { LoggerProvider } from './logger.provider';
import { Logger } from './logger.service';
@Global()
@Module({
providers: [LoggerProvider, Logger],
exports: [Logger, LoggerProvider],
})
export class LoggerModule {}
logger.provider.ts
:import { Injectable } from '@nestjs/common';
import { ConfigService } from '~lib/config';
import { Logger } from './logger.service';
import { LoggerEnum } from './logger.enum';
@Injectable()
export class LoggerProvider {
private loggerList: { [name: string]: Logger } = {};
constructor(private readonly configService: ConfigService) {}
getLogger(name: LoggerEnum = LoggerEnum.DEFAULT): Logger {
const logger = this.loggerList[name];
if (logger) {
return logger;
}
return this.instantiateLogger(name);
}
instantiateLogger(name: LoggerEnum): Logger {
const logger = new Logger(this.configService, name);
this.loggerList[name] = logger;
return logger;
}
}
Since both are global
modules, Nest should resolve the dependency and be able to launch.
With previous minor Nest versions (still v6), I had no problem. So maybe there is a recent change causing the issue.
In your ConfigService
line number 8 change the line from
import { LoggerEnum } from '~/lib/logger';
to
import { Loggerenum } from '~/lib/logger/logger.enum';
I'm not sure what changed recently, but if there is a circular dependency between files (not necessarily services, but files), it starts having this issue of being unable resolve providers. Here was a StackOverflow issue on it as well
I'm not sure what changed recently, but if there is a circular dependency between files (not necessarily services, but files), it starts having this issue of being unable resolve providers
Nothing related to this has changed recently. This issue may rather come from Node.js (and how it loads circular deps between files). In @LandazuriPaul case, the issue is very likely caused by module/service exported from the index.ts
. I would suggest avoiding barrel files (unless it's impossible).
Thank you @jmcdo29 and @kamilmysliwiec for your very quick responses and in general for your work on this awesome open source project!
Indeed it seems like barrel files are causing much trouble, it's not the first time I encounter weird issues because of them. I'll try and stick to your advice @kamilmysliwiec.
Most helpful comment
Nothing related to this has changed recently. This issue may rather come from Node.js (and how it loads circular deps between files). In @LandazuriPaul case, the issue is very likely caused by module/service exported from the
index.ts
. I would suggest avoiding barrel files (unless it's impossible).