21

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?

3
  • DEBUG=* node app will work at the command line: it'll first set an env variable, and the execute the command. Have you tried it? Commented Jun 21, 2014 at 23:05
  • @TravelingTechGuy In Windows, it tells me that DEBUG is not a command. But now I tried it with MSYS and it works. I keep forgetting that system-dependent documentation of Node is usually tailored toward nix systems. Commented Jun 22, 2014 at 7:58
  • 3
    agree about documentation not really covering/caring about Windows. Try using the set command, as in set DEBUG=* & node app Commented Jun 22, 2014 at 17:29

7 Answers 7

29

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!

Sign up to request clarification or add additional context in comments.

8 Comments

The official documentation also now includes a note for windows, suggesting to just use set and then run node afterwards. Thanks!
I'm having a similar issue on OSX if I include the DEBUG= line on my node start up command everything works. Id like to use the process.env.DEBUG though for ease of use, this isnt being picked up by the debug module. I've checked the var and its being set properly.
@Joel If you're trying to manually set fields on process.env, I'm not surprised it doesn't work. process.env is designed to be an interface to access your environment variables, so if you don't want to include the DEBUG= bit before running your Node app, just set the environment variable. macOS example: export DEBUG=express*
Thanks, no I'm not trying to manually set fields on process.env.
@Joel Could you explain your issue more clearly then? I'd love to help. In macOS (and other UNIXy systems) you can set environment variables in many ways, and no matter which way you do it, process.env should allow you to access the value. The "debug" Node module absolutely uses process.env to pick up the DEBUG variable you provide when launching your Node app.
|
17

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"
},

Comments

4

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');

Comments

3

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

6 Comments

Again.. this does NOT work on Windows. Please consider deleting or modifying your answer, as it does not contribute anything... Sorry...
@Domi If you give me some time i'll check it out for windows :) it's been a long 2 years so maybe something changed, i'll take a look and modify it again :) I was a little junior programmer, so those 2 years will maybe make from this a better answer :)
It seems like cross-env works without any extra files. Just need to modify the start command to be cross-env DEBUG=http,worker node example/app
@user1278519 are you sure you did not install it globally and that is why is working without the npm install cross-env?
@AlejandroVales Oh, I misunderstood. Some answers were simply discussing how to get the SET to work -- which was my issue. I had thought that your files with debug were somehow necessary to make it work. In short, I had to do exactly as you wrote at the end of your answer. I was confused because I was trying to access pre-existing debug information, e.g. express/knex. 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"
|
1

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.

Comments

1

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

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

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.