1

I am trying to use dotenv to manage configurations for different environment. At the moment, I only have development environment. To test dotenv library, I have .env and .env.development files in the root folder.

In .env, I have

PORT=5001

In .env.development, I have

PORT=5002

In index.js, if I have require('dotenv').config(), .env file is loaded and I can see process.env.PORT is 5001.

If I have require('dotenv').config({path: '.env.'+process.env.NODE_ENV}), and I also have NODE_ENV=development node index.js, then .env.development is not loaded.

However, if I have require('dotenv').config({path: '.env.development'}), .env.development file is loaded correctly.

Can I please get some help? Thank you

Update

The test code I am having is

const dotenv = require('dotenv');
const environment = process.env.NODE_ENV || 'development';
console.log(environment);
const dotenvPath = '.env.'+environment;
console.log(dotenvPath);
dotenv.config({path: dotenvPath});

console.log(process.env.PORT);
console.log(process.env.NODE_ENV);

and I am in Windows.

When I run set NODE_ENV=development & node index.js, the console log is

In CMD:

D:\>set NODE_ENV=development &  node index.js
development
.env.development
undefined
development

In Powershell, it is correct.

PS D:\> $env:NODE_ENV='development'; node index.js
development
.env.development
5001
development

I guess when I put the command as script in package.json, the command is run in CMD, rather than in Powershell, even though I run npm run start:development in Powershell.

Ben
  • 957
  • 1
  • 11
  • 37

4 Answers4

1

Instead of using

set NODE_ENV=production && node index.js

Please use

set NODE_ENV=production&&node index.js

(without redundant whitespace)

0

Well, you can access process.env variables AFTER dotenv package is executed.

So, when you pass a .env variable to it's config, it's still not available for the server.

NeNaD
  • 18,172
  • 8
  • 47
  • 89
  • Hi @NeNaD, can you please explain a bit more? and do you have any idea how to resolve it? I added some update in my post. Thank you – Ben Aug 29 '22 at 15:41
0

It is possible. I tried in my local following this post and everything worked as expected.

Toggle between multiple .env files like .env.development with node.js

Adding a screenshot for the proof of work Code With Output

  • Hi @Jagraj Singh, thank you for your example. For some reason, I am still not able to get it working. Please see the updated example in my post. – Ben Aug 29 '22 at 15:42
  • For CMD can you try this once? D:\>set NODE_ENV=development && node index.js @Ben – Jagraj Singh Aug 29 '22 at 15:48
  • Hi @Jagraj Singh, thank you for you advice. However, I get the same result. I tried to run `set NODE_ENV=development && node index.js` directly in CMD, or create a new script `"start:development:windows": "set NODE_ENV=development && node index.js",` and run `npm run start:development:windows` in Terminal of vscode. – Ben Aug 30 '22 at 14:46
  • Thank you @Jagraj Singh, I found the reason. – Ben Aug 30 '22 at 15:04
0

Finally, I find the reason.

The way I debug it is to have dotenv.config({path: dotenvPath, debug: true}); in my code and then when I run set NODE_ENV=development & node index.js in CMD, I get

development 
.env.development 
[dotenv][DEBUG] Failed to load .env.development  ENOENT: no such file or directory, open '.env.development '
undefined
development

I notice the space in the file name, which leads to the .env.development could not be found.

Then I change the command to set NODE_ENV=development&node index.js, then it starts working.

development
.env.development
5001
development
Ben
  • 957
  • 1
  • 11
  • 37