Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with dotenv #446

Closed
hyperh opened this issue Apr 26, 2017 · 5 comments
Closed

Not working with dotenv #446

hyperh opened this issue Apr 26, 2017 · 5 comments
Assignees
Labels
discussion This issue is requesting comments and discussion wont-fix This is not a feature or proposal that will be incorporated, or a bug that won't be addressed

Comments

@hyperh
Copy link

hyperh commented Apr 26, 2017

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
@thebigredgeek thebigredgeek added discussion This issue is requesting comments and discussion wont-fix This is not a feature or proposal that will be incorporated, or a bug that won't be addressed labels Apr 26, 2017
@thebigredgeek thebigredgeek self-assigned this Apr 26, 2017
@danielkcz
Copy link

danielkcz commented 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.

@thebigredgeek
Copy link
Contributor

thebigredgeek commented 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.

@thebigredgeek
Copy link
Contributor

thebigredgeek commented Apr 27, 2017

fixed with 2.6.5

iwaduarte added a commit to iwaduarte/express-sire that referenced this issue Oct 13, 2022
- by using debugFactory.debug as described here debug-js/debug#446 (comment)
@iwaduarte
Copy link

iwaduarte commented Oct 13, 2022

@thebigredgeek It took me a few years to find this comment. Most people just ignore it and replace it eventually if they use the dotenv approach. I guess this should be added to the documentation. From your tip @thebigredgeek a working example should be:

  [esm]
import  dotenv from "dotenv";
dotenv.config();

import debugFactory from 'debug';
const { DEBUG } = process.env;
debugFactory.enable(DEBUG)

const debug = debugFactory('whatever');
debug("Hello dotenv");

Also, note that the env should have an extra space at the end. (bug?)

//ENV file
DEBUG= "variable:* "

Otherwise, it won't work.

iwaduarte added a commit to iwaduarte/express-sire that referenced this issue Oct 13, 2022
- by using debugFactory.debug as described here debug-js/debug#446 (comment)
@Qix-
Copy link
Member

Qix- commented Oct 13, 2022

Also, note that the env should have an extra space at the end. (bug?)

We have nothing to do with the dotenv package. It's a hack that is not recommended to be used at all. The dotenv repo has had lots of issues with debug and yet we do nothing strange or out of the ordinary. Your environment should be established before the process is executed. If this means using a wrapper process to read a .env file and then execute a process with those variables, great - that would fix literally 100% of your environment-variable-related problems, 100% of the time, because there's no race condition in Node.

Further, if you're importing dotenv directly, you are almost definitely writing brittle, magic code.

Please just heed this advice. Establish your environment variables before node ever executes, however that might look. You'll be much happier in the long run.

@debug-js debug-js locked as resolved and limited conversation to collaborators Oct 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
discussion This issue is requesting comments and discussion wont-fix This is not a feature or proposal that will be incorporated, or a bug that won't be addressed
Development

No branches or pull requests

5 participants