I'm on a Windows 7 machine. I've tried running the following scenario via cmd and also Git bash, getting the same result in both cases - undefined env variable after loading.
process.env.ENVIRONMENTAL_VARIABLE_FULL_PATH has the expected value (like C:\Users\me\workspace\myproject\env\env.localhost-windows), but process.env.DB_HOST is unexpectedly undefined.
package.json
...
"server-windows": "set DOTENV_FILE=env.localhost-windows && node server",
...
"dotenv": "^6.2.0",
server.js
process.env.ENVIRONMENTAL_VARIABLE_FULL_PATH = path.resolve('./env/', process.env.DOTENV_FILE);
require('dotenv').config({ path: process.env.ENVIRONMENTAL_VARIABLE_FULL_PATH });
console.log(
'ENVIRONMENTAL_VARIABLE_FULL_PATH in server.js immedately after require dotenv: ',
process.env.ENVIRONMENTAL_VARIABLE_FULL_PATH
);
console.log('DB_HOST in server.js immedately after require dotenv: ', process.env.DB_HOST);
/env/env.localhost-windows
_DB_PASSWORD=set in AWS ECS task definition container settings
_DOTENV_FILE=set in AWS ECS task definition container settings
DB_USERNAME=docker
DB_HOST=db
DB_DATABASE=docker
ECS_LOGLEVEL=debug
NODE_ENV=development
command run:
npm run server-windows
Closing as the issue is found within the package.json command:
"server-windows": "set DOTENV_FILE=env.localhost-windows && node server"
CLI is interpreting the space character after the word windows and throwing file not found because there is a space at the end.
I debugged this issue using fs.readFileSync, which threw an informative error. As an enhancement, dotenv could similarly throw:
ENOENT: no such file or directory, open 'C:\Users\jvandivier\workspace\pamplemousse\env\env.localhost-windows '
This issue is still relevant.
It seems dotenv does not work on Windows out of the box.
This should be documented with workarounds / solutions like directly parsing the file.
Can you reopen the issue? In my opinion we should have a crossplatform solution (no extra scrip just for Windows).
Reopened per request but I have little interest in providing additional information to field the issue and I haven't tested with recent dotenv versions.
What I can say is that I still think it would be a good idea to through a more informative error in the situation originally described.
Most helpful comment
This issue is still relevant.
It seems dotenv does not work on Windows out of the box.
This should be documented with workarounds / solutions like directly parsing the file.
Can you reopen the issue? In my opinion we should have a crossplatform solution (no extra scrip just for Windows).