Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global module provider injection issue #4873

Open
zyulyaev opened this issue Jun 2, 2020 · 7 comments
Open

Global module provider injection issue #4873

zyulyaev opened this issue Jun 2, 2020 · 7 comments
Labels

Comments

@zyulyaev
Copy link

zyulyaev commented Jun 2, 2020

Bug Report

Current behavior

The issue arises in the following setup:
AppModule imports ModuleA(global) and ModuleB(non-global) and ConfigModule.forRoot,
ModuleA declares some REQUEST-scoped provider X with request context being injected,
ModuleB imports ConfigModule.forFeature, and declares some provider Y, without specifying a scope, with X and config being injected.

In such setup constructor for X injected into Y is not called (in case X is declared using class).
If X is declared via useFactory, null is injected into Y.

Input Code

https://github.com/zyulyaev/nest-global-module-issue

Expected behavior

X's constructor is called and its dependencies are injected.

Environment


Nest version: 7.1.2
 
For Tooling issues:
- Node version: v12.15.0
- Platform: macos

Others:
package manager: pnpm
@zyulyaev zyulyaev added the needs triage This issue has not been looked into label Jun 2, 2020
@zyulyaev
Copy link
Author

zyulyaev commented Jun 2, 2020

There are several workarounds:

  1. explicitly import ModuleA into ModuleB,
  2. or declare Y's scope = REQUEST,
  3. or remove config from Y's constructor parameters.

@abbasvalliani
Copy link

Any updates on this? I am running into a similar issue.

@kamilmysliwiec kamilmysliwiec added effort2: days priority: medium (3) Medium priority issue that needs to be resolved labels Feb 3, 2021
@uamanager
Copy link

uamanager commented Feb 28, 2021

Hey guys, I'm facing same issue when using Request Scoped Provider even not directly in Global Guard. Constructor of this Global Guard is not called and canActivate method throwing errors because DI is not working.

I need Request Scoped Provider to gather temporary data from request. If there is another way to do this, until issue is fixed - LMK. (Param decorator is not a solution in my case)

@jmcdo29
Copy link
Member

jmcdo29 commented Feb 28, 2021

nestjs/graphql#325 may have a solution you're looking for, using ModuleRef andContextIdFactory

@KostyaTretyak
Copy link

KostyaTretyak commented Nov 21, 2021

If in NestJS DI works similarly to Angular DI, then the injectors are connected by a hierarchical connection. Therefore, from the lower scope (request) you can ask providers from the higher scope (application), but not vice versa.

As far as I understand, you are trying to get from the application scope the request scope. Yes? If so, then of course you need:

declare Y's scope = REQUEST,

And you can remove request scope from X provider.

@micalevisk micalevisk moved this to 🤔 I'll investigate later in My contributions to NestJS framework 😻 Dec 17, 2021
@micalevisk micalevisk moved this from 🤔 I'll investigate later to 💭 Thinking on take this in My contributions to NestJS framework 😻 Dec 19, 2021
@micalevisk micalevisk moved this from 💭 Thinking on take this to 👀 not sure if I can help in My contributions to NestJS framework 😻 Dec 26, 2021
@KOCEAN33
Copy link

KOCEAN33 commented Nov 3, 2023

I also faced the same error.
The same thing happens on my Mac/Windows, and nestjs comfigure.forRoot doesn't load any env files. I thought the problem was updating to pnpm 8.10, but looking at your date, I don't think it's a version problem.

so i'm using dotenv on packages.json to use .env

@RobbyKetchell
Copy link

As @KostyaTretyak mentions, the dependency injection hierarchy means that you will need to add an import statement in ModuleB to import ModuleA. You mentioned this as well in your "work arounds."

I can reproduce your error from your input code, and resolve it by adding GlobalModule in the imports for the ChildModule:

imports: [
    ConfigModule.forFeature(childConfig),
    GlobalModule
  ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants