6

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'));
Platus
  • 1,753
  • 8
  • 25
  • 51
  • 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 Apr 26 '20 at 16:08

1 Answers1

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)
Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94