Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda