question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not working with dotenv

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
thebigredgeekcommented, Apr 27, 2017

@FredyC @hyperh We get the environment variable within the load function in node.js and browser.js, example. We immediately call enable, passing the result of load() here… so all of the environment variables are uptaken when debug is first imported. You should be able to call debug.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 call enable.

1reaction
danielkczcommented, Apr 27, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found