Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to globally inject value across modules in NestJS?

Tags:

nestjs

nrwl

I'm working with nx workspace and nestjs. I would like to inject a value across multiple modules in nestjs app.

Final goal is to reproduce similar way of configuration management as vsavkin mentioned for Angular

But it seems it's not possible, or I missed something.

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

How can I notify FeatureModule it needs to access to this global injected value ?

This is working fine inside AppService (service in root module), but not in any sub modules.

Here is my code below. Or an full example on codesandbox.io

app.module.ts

@Module({
  imports: [
    FeatureModule
  ],
  controllers: [
    AppController
  ],
  providers: [
    AppService,
    {
      provide: 'MY-TOKEN',
      useValue: 'my-injected-value',
    }
  ],
})
export class AppModule {}

feature.module.ts

@Module({
  imports: [],
  controllers: [],
  providers: [
    FeatureService
  ],
})
export class FeatureModule {
}

feature.service.ts

@Injectable()
export class AppService {
  constructor(
    @Inject('MY-TOKEN') private injectedValue: string
  ) {}
}
like image 551
Thierry Falvo Avatar asked Mar 29 '19 07:03

Thierry Falvo


1 Answers

Quote from NestJS official documentation:

In Angular, the providers are registered in the global scope. Once defined, they're available everywhere. On the other hand, Nest encapsulates providers inside the module scope. You aren't able to use the module providers elsewhere without importing them. But sometimes, you may just want to provide a set of things which should be available always - out-of-the-box, for example: helpers, database connection, whatever. That's why you're able to make the module a global one.

So what you can do is just defining one global module with that MY-TOKEN provider:


@Global()
@Module({  
  providers: [
    {
      provide: 'MY-TOKEN',
      useValue: 'my-injected-value',
    }
  ],
  exports: ['MY-TOKEN'],
})
export class GlobalModule {}

and then you can use its exported values without importing the global module anywhere.

like image 90
Karol Samborski Avatar answered Oct 15 '22 15:10

Karol Samborski