Skip to content Skip to sidebar Skip to footer

Process.env.varible Undefined In Nest.js

I am using nest.js and I want to import object defined in auth.parameters.ts file. File is defined as below. It appears environmental variable is not visible unless below variable

Solution 1:

As you're using Nest's ConfigModule, then unless you're creating a custom config file you shouldn't be doing anything with process.env. Instead, you should be injecting Nest's ConfigService where you need the environment variables and using this.configService.get('CUSTOM_VARIABLE') (or whatever value you need). To get started with this, you need to import the config module

import { Module } from'@nestjs/common';
import { ConfigModule } from'@nestjs/config';

@Module({
  imports: [ConfigModule.forRoot()],
})
exportclassAppModule {}

You can pass { isGlobal: true } to the forRoot() call to make the ConfigService injectable anywhere in your application.

If you want a different approach where you can use the process.env object instead, you can add this as the first two lines of your main.ts

import { config } from'dotenv';
config();

Nest calls this (or something similar) under the hood in the ConfigService, but that doesn't make it immediately available.

Post a Comment for "Process.env.varible Undefined In Nest.js"