Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
dotenv does not load when path has variable

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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!