Not working with dotenv
See original GitHub issueI can’t seem to get debug
working with dotenv
. It works fine when I set the env var from outside of .env like so:
package.json
"dev": "NODE_ENV=development DEBUG=*,apicache nodemon",
npm run dev
server.js
import 'babel-polyfill';
import makeDebug from 'debug';
import dotenv from 'dotenv';
const env = dotenv.config();
console.log(process.env.DEBUG); // outputs *,apicache correctly
const debug = makeDebug('brainfm:server/index');
debug('.env', env); // Doesn't show anything
.env
DEBUG =*,apicache
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
dotenv file is not loading environment variables - Stack Overflow
This solved my issues in Node v8.14.1 : const path = require('path') require('dotenv').config({ path: path.resolve(__dirname, '../.env') }).
Read more >5 reasons why your .env environment variables don't work
1. Your framework doesn't automatically load .env files. · 2. You added or otherwise updated your .env file after starting your server. ·...
Read more >npm dotenv environment variables not recognized in nodejs ...
In each of the config files I am not able to access any properties of the process.env object that are defined in the...
Read more >motdotla/dotenv: Loads environment variables from .env for ...
Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env . Storing configuration in the environment separate from ......
Read more >Dotenv - npm
Start using dotenv in your project by running `npm i dotenv`. There are 31886 other projects in the npm registry using dotenv.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@FredyC @hyperh We get the environment variable within the load function in
node.js
andbrowser.js
, example. We immediately callenable
, passing the result ofload()
here… so all of the environment variables are uptaken when debug is first imported. You should be able to calldebug.enable
and pass whatever pattern you want at runtime if you are dynamically intaking environment variables as shown here, but make sure to do this before any debug instances are created as it won’t work for instances that are created after you callenable
.Isn’t this more likely an issue of timing when env is initialized by dotenv and read by debug? In your example you are importing debug and then later configuring dotenv. I am not sure where this is actually handled on debug side, I don’t actually see it in a source code.
I recommend adding npm script like
"start": "node -r dotenv/config -r babel-polyfill server.js"
. It is a generally better approach like instead of hardwiring these things into code.