'express debug module not working

I'm on Windows trying to use the debug module https://www.npmjs.org/package/debug

I installed express-generator

var debug = require('debug')('MyApp');
debug('log'); // I don't see this on console

I tried to debug the variable

console.log(debug); // I get function disabled() {}

How to enable it? shouldn't be enabled by default?



Solution 1:[1]

Output from debug functions created by the debug module are only displayed when you set the appropriate environment variable when starting your script. That is what lets you selectively enable debug output so that it's not an all or nothing method of displaying debug information. This is similar to how node.js core works for showing internal debug information on the console.

So in your example you would need to execute this at your shell prompt: DEBUG=MyApp node foo.js, where foo.js is your script containing var debug = require('debug')('MyApp');.

For Windows you'd need to do set DEBUG=MyApp on the command line, followed by node foo.js.

Solution 2:[2]

In case you want to set it in your js code,

myapp-debug.js

#!/usr/bin/env node

process.env['DEBUG'] = 'myapp:server';
var debug = require('debug')('myapp:server');

// ...
  • I find it useful when there's a single shebanged executable file (on windows you'l need to set node to open .js files)

Solution 3:[3]

On windows set DEBUG=* or your configured name for debug e.g. myApp. In all js files whenever I want debug I configured just like following statement.

var debug = require('debug')('MyApp'); 

require('debug')('MyApp'); //This is very imp. You should configured some name for debug

while executing on windows

D:>set DEBUG=MyApp

D:>node <your app file>.js 

D:> node foo.js

Solution 4:[4]

I was also faces the same issue. Finally i get the solution that In windows

set DEBUG=* & node index.js

above command has to work. But in my this is not working on VS code terminal. But if is you write this command in scripts section of your package.json then it will work.

"start": "set DEBUG=app & node app.js"

Now you just need to write

npm start

on terminal and there you go

Solution 5:[5]

Windows does not set environment variables the way Linux and others do.

The solution is:

set DEBUG=my-application
npm start

Solution 6:[6]

I was trying to figure this issue out as well and it seems to me (at least on my Windows 10 machine) that the way to do this (at least from a non-linux command line; I am using the WebStorm build-in command line) is to include the '&' operator. So write something like this:

set DEBUG=my-app & node index.js

Solution 7:[7]

Make sure that process.env.DEBUG has been set before you create the debug logger.

Solution 8:[8]

Just in case you cannot run the express debug in terminal using

set DEBUG=express:* & node index.js

on Windows, put

"this": "set DEBUG=express:* & node --experimental-json-modules index.js"

inside "scripts" in package.json, then type

npm run this

in terminal. Thanks for reading!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jossef Harush Kadouri
Solution 3 Nikhil
Solution 4 Vasudev Singhal
Solution 5 Nizar Blond
Solution 6 user2403232
Solution 7 Gilbert
Solution 8 Toni Bui