Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

3.5K
Visualizações
How to use global variables in nest.js?

Taking configuration as an example, the Nest.js documentation advocates registering Config Modules and injecting them into other modules in a dependency injection way.

The benefits are obvious, and the dependencies and code are clear, but what if I have a nest.js project that needs to invoke the configuration information at startup? This actually caused me trouble.

My idea is to use a store (actually a closure) to manage all the variables that might be needed globally, the client-side link objects, registered at startup, and introduced when needed.

When corresponding variables are registered in this way, they can be introduced anywhere. The drawback is that you need to manage dependencies yourself.

With the above concept design of demo: https://github.com/sophons-space/nest-server.

Please e help me correct, I am still a rookie.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

If you want to use Nest flow it should be defined in the configuration file

// app.module.ts
import configuration from './config/configuration';

imports: [
// first import as first initialization
  ConfigModule.forRoot({
    isGlobal: true, // to get access to it in every component
    load: [configuration],
  }),
]
...
// configuration.ts
export default (): any => {
  return {
    someGlobalConfigVariable: parseInt(process.env.PORT, 10) || 3000,
  };
};

over 4 years ago · Santiago Trujillo Relatório

0

The way you can approach this is similar to how NestJS libraries or integrations usually handle configuration; using a method on the base module.

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  // Note the `configure`-method
  const app = await NestFactory.create(AppModule.configure({
    myConfig: 'value',
  });
  await app.listen(3000);
}
bootstrap();

app.module.ts

import { DynamicModule } from '@nestjs/common';


export class AppModule {
  static configure(config): DynamicModule {
     return {
       module: AppModule,
       providers: [{ provide: 'CONFIG', useValue: config }],
       // ....
     }
  }
}
over 4 years ago · Santiago Trujillo Relatório

0

Create a file global.service.ts (inside a folder you can name it utils or whatever) & put the code bellow

export class GlobalService{ 
   static globalVar: any; 
}

Set value to the globalVar

GlobalService.globalVar = 'some value';

Get value from globalVar

console.log(GlobalService.globalVar);

N.B. Don't forget to import GlobalService wherever you want to use.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda