Explanation for what 'DEBUG=myapp:* npm start' is actually doing

Viewed 9335

The getting started page for the Express application generator (located here) says to start the server by using $ DEBUG=myapp:* npm start if using MacOS or Linux.

This works fine, but I'm having trouble understanding what this line is actually doing. I would have guessed that it would be assigning something to the variable DEBUG, but after exiting the server I ran echo $DEBUG and it printed nothing. I'm guessing the : is somehow key here, but it's not clear to me how/why.

Hopefully someone can break this down for me.

6 Answers
  • DEBUG=myapp:* npm start consist of two parts.

  • The first part is DEBUG=myapp:* and the second part is npm start

  • You may run DEBUG=myapp:* first in your command-line tool, and then followed by running npm start.

  • DEBUG=myapp:* means you are telling nodejs that you want to turn on logging for debugging purposes.

    • Remember to replace myapp with your app name. You can find your app name in package.json file under "name" property. enter image description here
    • The * in myapp:* means to see all the internal logs used in Express
    • If you only want to see the logs from the router implementation, then set the value of DEBUG to myapp:router. Likewise, to see logs only from the application implementation set the value of DEBUG to myapp:application, and so on.
  • npm start is telling npm to run your scripts stated in the package.json file and the script name is called start enter image description here

  • Source: https://expressjs.com/en/guide/debugging.html

The : is just text that gets assigned to the env variable.

What the line is doing and how it works is as follows:

First I'll discuss express internal level debug messages. The following env setting all display the same internal log level

DEBUG=express:*, npm start
DEBUG=express.*, npm start
DEBUG=express*, npm start

And the following setting displays the internal log level messages for just the internal router

DEBUG=express:router, npm start
DEBUG=express.router, npm start

Now in order to see "application" level DEBUG statements. Those that you've added to your code with the debug function.

It has to do with your module dependencies. The value(s) passed into the debug package. The values can be any arbitrary name - that is they don't need to match package.json, file, object variable name, etc. However the env variable DEBUG value needs to correspond to the value passed into the debug package in your code.

This line of code

var debug = require('debug') ('myapp:myapp');

Consider these two files

www
…
var debug = require('debug')('myapp:www');

app.js
…
var debug = require('debug')('myapp:app');

Then if we run the following scenarios:

  1. DEBUG=myapp:* npm start
  2. DEBUG=myapp:www npm start
  3. DEBUG=myapp:app npm start

We get the following:

screen shot

vagrant@n1:~/despobaldo/server$ DEBUG=myapp:* npm start

> server@0.0.0 start /home/vagrant/despobaldo/server
> node ./bin/www

  myapp:app here I am +0ms
  myapp:www Listening on port 3000 +8ms
^Cvagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$ DEBUG=myapp:www npm start

> server@0.0.0 start /home/vagrant/despobaldo/server
> node ./bin/www

  myapp:www Listening on port 3000 +0ms
^Cvagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$ DEBUG=myapp:app npm start

> server@0.0.0 start /home/vagrant/despobaldo/server
> node ./bin/www

  myapp:app here I am +0ms
^Cvagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$

In order to see both internal and "application" level logging you would doing something like this:

DEBUG=myapp:*,express:router npm start

screen shot

vagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$ DEBUG=myapp:*,express:router npm start

> server@0.0.0 start /home/vagrant/despobaldo/server
> node ./bin/www

  express:router use '/' query +0ms
  express:router use '/' expressInit +4ms
  express:router use '/' logger +0ms
  express:router use '/' jsonParser +7ms
  express:router use '/' urlencodedParser +3ms
  express:router use '/' cookieParser +0ms
  express:router use '/' serveStatic +1ms
  myapp:app here I am +0ms
  express:router use '/' router +1ms
  express:router use '/users' router +0ms
  express:router use '/' <anonymous> +0ms
  express:router use '/' <anonymous> +0ms
  myapp:www Listening on port 3000 +7ms
^Cvagrant@n1:~/despobaldo/server$
Related