Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
Globle module without import not working nestjs

I am trying to implement a global module in nest.js

I have created a service like below

export interface ConfigData {
    DB_NAME:        string;
}

@Injectable()
export class ConfigManager {
    private static _instance?: ConfigManager;
    public static configData:ConfigData | null;
    private constructor() {
        console.log(ConfigManager)
        if (ConfigManager._instance)
            ConfigManager._instance = this;
        else{
            ConfigManager._instance = new ConfigManager()
            ConfigManager.configData = <ConfigData|null><unknown>ConfigManager.fetchSecretData()
        }
    }
    private static async fetchSecretData():Promise<ConfigData|null>{
        // some data from db
    }
    // static get instance() {
    //     return ConfigManager._instance ?? (ConfigManager._instance = new ConfigManager());
    //     //return ConfigManager._instance ?? (ConfigManager._instance = ConfigManager.fetchSecretData()) //new ConfigManager());
    // }
    
}

configuration.module.ts

@Global()
@Module({
    providers: [ConfigManager],
    exports: [ConfigManager],
})
export class ConfigurationModule {}

and in app.module.ts added ConfigurationModule in imports.

Also adding private constructor on service unable it to add in module.ts file.

I am expecting that I should be able to configData anywhere without importing the ConfigManager. but it's not working...

ConfigManager is not available without import.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You've only marked your module with @Global decorator, but the NestJS needs to somehow initialize that module and make it globally available.

What this means is that you have to add this module to your core application module and NestJS will do the rest for you, so something like this (or however your root module is named):

@Module({
  imports: [ConfigurationModule],
})
export class AppModule {}

From the documentation

The @Global() decorator makes the module global-scoped. Global modules should be registered only once, generally by the root or core module.

about 4 years ago · Juan Pablo Isaza Report

0

this is 'per-design' of es6/ts: you cant use the class without importing it.

you are mixing the concepts of di (instantiation/composition) with importing (defining which classes are available in the module scope)

about 4 years ago · Juan Pablo Isaza Report

0

Global modules in nest only means you don't have to include that module in the imports: [] of every other module that requires it's providers. The providers in the global module still behaves as as normal providers i.e. you need to inject it where you need it.

So in your case since you have already added @Global() to ConfigManager and imported ConfigurationModule in app.module.ts, you will not need to add ConfigurationModule in imports for any other modules who want to use ConfigManager. You would, however, still need to inject the ConfigManager provider - that's what @Injectable() means :).

To do the injection, you need a constructor(private configManager: ConfigManager) {} in the class that needs to use ConfigManager, and since you need access to the type of the class, you'll need import { ConfigManager } from '../path/to/ConfigManager' as well.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!