#using guard from external library

6 messages · Page 1 of 1 (latest)

paper socket
#

i added a auth guard to my project and it worked fine but when i moved it to external library (to use it in different nest services) i got an error:

Nest can't resolve dependencies of the FronteggGuard (?). Please make sure that the argument Reflector at index [0] is available in the TasksModule context.

Potential solutions:

  • Is TasksModule a valid NestJS module?
  • If Reflector is a provider, is it part of the current TasksModule?
  • If Reflector is exported from a separate @Module, is that module imported within TasksModule?
    @Module({
    imports: [ /* the Module containing Reflector */ ]
    })

Error: Nest can't resolve dependencies of the FronteggGuard (?). Please make sure that the argument Reflector at index [0] is available in the TasksModule context.

how i can make it work?
i added the external library using yarn add file: ...

reef ice
#

File links don't work well during local development with nest because of the way instanceof checks work.. You can use something like yalc for local development, or copy the package.json and dist of your library to node_modules/<package_name> of your application

paper socket
#

thanx, i will try to do that

paper socket
reef ice
#

So, nest uses instanceof checks on class based providers to determine if it already knows the provider or not and keeps a map of token to value cache so it knows quickly which value to inject.

When there are multiple import locations for a module, like @nestjs/core in two different workspaces, because peerDeps don't get properly respected with a file:// or link protocol via yarn, instanceof Reflector will come back false because of how realms work in JavaScript. You'll essentially get Reflector instanceof Reflector comes out to false

mellow estuary
#

in short, the Reflector class is declared at @nestjs/core package, so if you have the following structure:

.
app/
└── node_modules/
    ├── @nestjs/core
    └── lib/
        └── node_modules/
            └── @nestjs/core

the Reflector at app/node_modules/@nestjs/core isn't strictly the same as the one at app/node_modules/lib/node_modules/@nestjs/core

You need to get ride of the lib/node_modules/@nestjs/core module in order to solve this.