Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

3.5K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda