admin管理员组

文章数量:1318564

I'm trying to set configuration variables on my project using the official documentation.

I added the following line to my app.module.ts imports:

ConfigModule.forRoot({
  isGlobal: true
}),

I created a .env file at the root of my project with the following content:

MY_VARIABLE=myself

And I use dependecy injection to get access to the configuration service:

constructor(private configService: ConfigService) {}

However the following line logs 'Env variable: undefined'

console.log('Env variable: ', this.configService.get<any>('MY_VARIABLE'));

I'm trying to set configuration variables on my project using the official documentation.

I added the following line to my app.module.ts imports:

ConfigModule.forRoot({
  isGlobal: true
}),

I created a .env file at the root of my project with the following content:

MY_VARIABLE=myself

And I use dependecy injection to get access to the configuration service:

constructor(private configService: ConfigService) {}

However the following line logs 'Env variable: undefined'

console.log('Env variable: ', this.configService.get<any>('MY_VARIABLE'));
Share Improve this question asked Apr 26, 2020 at 9:45 PlatusPlatus 1,5079 gold badges29 silver badges59 bronze badges 1
  • Sounds like you've got it set up properly. Can you share a reproduction? In the snippets you've provided I can't see a problem. – Jay McDoniel Commented Apr 26, 2020 at 16:08
Add a ment  | 

2 Answers 2

Reset to default 5

Several scenarios:

  • You don't have .env file
  • .env is incorrectly defined
  • You're trying to access environmental variables from .env located outside the server's root path
  • You need to install dotenv package
  • Deploying to Serverless and in the functions (or other deployable folder) the .env is missing (even though it's in the root location it has to be there too)

I have the same problem. Then I figure out that I have imported process from import process from 'node:process'; this is not needed. After removing this its working fine.

本文标签: javascriptNestJS env variable undefinedStack Overflow