Nest: Global module not resolved

Created on 25 Feb 2020  路  3Comments  路  Source: nestjs/nest

Bug Report

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.

Current behavior

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 */ ]
  })

Input Code

  • The config.module.ts:
import { Module, Global } from '@nestjs/common';

import { ConfigService } from './config.service';

@Global()
@Module({
  providers: [ConfigService],
  exports: [ConfigService],
})
export class ConfigModule {}
  • The 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 {}
  • The 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;
  }
}

Expected behavior

Since both are global modules, Nest should resolve the dependency and be able to launch.

Possible Solution

With previous minor Nest versions (still v6), I had no problem. So maybe there is a recent change causing the issue.

Environment

  • "@nestjs/common": "^6.10.14" resolved to "6.11.8"
  • "@nestjs/core": "^6.10.14" resolved to "6.11.8"
  • "@nestjs/cli": "^6.13.2" resolved to "6.14.2"

Minimal repository reproducing the issue

https://github.com/LandazuriPaul/nest-global-modules

needs triage

Most helpful comment

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).

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings