简体   繁体   中英

Node.js debug package not working with .env file

I want to use the debug package to log messages like this:

const debug = require('debug')('app:Debug');
debug('this is a log message!');

Then in package.json :

"scripts": {
    "dev": "DEBUG=* nodemon server"
}

And it worked perfectly.

Now I want to store all my environment variables in the .env file like so:

PORT=5000
NODE_ENV=development
DEBUG=*

And if I do this:

const debug = require('debug')('app:Debug');
const dotenv = require('dotenv');
dotenv.config();
debug('this is a log message!');

No message is logged. I checked the DEBUG environment variable with console.log('process.env.DEBUG') and it is set. I don't understand where the problem is.

Any help will be appreciated thanks.

You probably need to init dotenv before you setup the debug -module:

const dotenv = require('dotenv');
dotenv.config();
const debug = require('debug')('app:Debug');
debug('this is a log message!');

Nevermind guys. I removed the node_modules directory and re-installed the packages and everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM