Question

I am trying to figure out what is wrong with my sessions (using express-session), and I found that it uses the debug module. However, I can't seem to enable the debug messages. It says, debugging needs to be enabled through the DEBUG environment variable, but I can't seem to get it to run.

The tutorial in the README has this picture:

enter image description here

Under Windows I get "DEBUG is not a command for the command-line".

So I tried setting the environment variable explicitly using:

process.env.DEBUG = "*";

and still nothing.

What am I doing wrong?

Was it helpful?

Solution

As Traveling Tech Guy suggested in the comment, in a Windows command prompt, the proper syntax is:

set DEBUG=* & npm start

Obviously you can replace npm start with whatever command you need to launch your Node.js app. Just be sure to use the set command and don't forget the & between that command and the one launching your Node.js app!

OTHER TIPS

If you are used to powershell, I recommend this setup in your package.json, then you can just run npm start so you don't type all that out every time.

"scripts": {
    "start": "@powershell $env:DEBUG='*,-express:router*' ; node app.js"
},

Install debug package with npm inside the node application

npm install debug

Open a powershell and then

$Env:DEBUG="name_to_call"

node path_to_js_to_execute.js

Inside your pgm

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

Firstly you need to install the debug module using

"npm install debug --save"

you will see that the following lane has been added to you package.json (which has all the npm modules charged to your proyect)

then you need to add this to import the module to the file in which you want to run the debug

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

now we have to just put the message we want to write, for example hello

var debug = require('debug')('name_to_call');
debug('Hello');

(try pasting the code above directly to a file)

Now we just need to start the windows server with DEBUG, to do so we are going to use the npm package cross-env that will make easier to set an ENV variable across any operating system (platform agnostic)

npm install cross-env

Change the package.json and add the following under the scripts section

"start-server": "cross-env DEBUG=name_to_call node server.js"

Now to start the server just run the following in a command line (from the directory in which your project is) and you are good to go

npm run start-server

In Babun a windows shell, I run,

npm

npm install debug --save

babun

DEBUG=http node app

app.js

var app = express();
var debug = require('debug')('http');
var http = require('http').Server(app);

var server = http.listen(app.get('port'), function() {
    debug('listening on port ' + server.address().port);
});

works like a charme for me.

Windows Command: set DEBUG=* & node ./app.js

PowerShell: $env:DEBUG='*'; node app.js

Terminal/WSL (Ubuntu): DEBUG=* node ./app.js

There are various answers provided earlier to set the environment variable and I found that nothing is working in my Windows 11 machine. So, I tried to google how to set environment variable in windows using command prompt and I found that, using below command can set the environment variable in windows machine.

setx NODE_DEBUG = 'http' 

Above command will enable debug mode for http in node which you can check in your windows environment variable setting. you can use * instead of http if you want to enable debug mode for all node runs. To update this NODE_DEBUG value, we need to write like this

setx NODE_DEBUG ''

But a security warning will also come in debug mode like below. So, make sure to not keep this in production site.

(node:28928) Warning: Setting the NODE_DEBUG environment variable to 'http' can expose sensitive data (such as passwords, tokens and authentication headers) in the resulting log.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top