A tiny JavaScript debugging utility modelled after Node.js core's debugging technique. Works cross-platform in NodeJS and browsers.
npm install debug
Debug exports a function which can be provided with the name of your package. The function it returns is essentially a decorated version of console.error which you can pass debug statements to.
This allows you to toggle the debug output for different parts of your module as well as the module as a whole.
const debug = require('debug')('http')
const express = require('express')
const name = 'My App'
const app = express()
debug(`Booting ${name}...`)
app.get('/', (req, res) => {
debug(`${app} called!`)
res.send('Hello World!')
})
app.listen(8080, () => debug(`${name} listening on port ${port}!`))
require('./worker')
Example worker:
const a = require('debug')('worker:a')
const b = require('debug')('worker:b')
function work() {
a('doing lots of uninteresting work')
setTimeout(work, Math.random() * 1000)
}
work()
function workb() {
b('doing some work')
setTimeout(workb, Math.random() * 2000)
}
workb()
The DEBUG
environment variable is then used to enable these based on space or comma-delimited names or a wildcard:
Type: string
The name for debug
to identify itself as.
Type: object
An object of printf-style formatters which debug uses.
Officially supported formatters:
Formatter | Representation |
---|---|
%O |
Pretty-print an Object on multiple lines. |
%o |
Pretty-print an Object all on a single line. |
%s |
String. |
%d |
Number (both integer and float). |
%j |
JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
%% |
Single percent sign ('%'). This does not consume an argument. |
You can add your own custom formatters by extending it. For example, if you wanted to add support for rendering a Buffer as hex with %h
, you could do something like:
const createDebug = require('debug')
createDebug.formatters.h = (v) => {
return v.toString('hex')
}
Elsewhere in your code, you can do:
const debug = createDebug('foo')
debug('this is hex: %h', new Buffer('hello world'))
//=> foo this is hex: 68656c6c6f20776f726c6421 +0ms
Type: string
The namespace that was specified in the initialiser function.
Type: boolean
Default: (true
when enable via environmental variable)
Log debug messages.
Type: boolean
Default: (true
when supported)
Use colors in the log messages.
Type: number
Default: (If useColors
is true
, default to a random colour)
The color to use in the log messages.
Remove the debug function and allow it to be garbage-collected.
Extend the current namespace by appending it with another.
Type: string
The namespace to extend it by.
Type: string
Default: :
The deliminer to use for extending the namespace.
Type: object
Type: number
An array of colors which can be used in ANSI escape codes.
Type: boolean
Hide the date when not using colors.
Type: number
The difference in milliseconds since the last debug message.
Type: number
The timestamp on which the previous debug message was logged.
Type: number
The timestamp on which the current debug message was logged.
When running debug
in NodeJS, you can set a few environment variables that can change the behavior of the debug logging:
Name | Purpose |
---|---|
DEBUG |
Enables/disables specific debugging namespaces. |
DEBUG_HIDE_DATE |
Hide date from debug output (non-TTY). |
DEBUG_COLORS |
Whether or not to use colors in the debug output. |
DEBUG_DEPTH |
Object inspection depth. |
DEBUG_SHOW_HIDDEN |
Shows hidden properties on inspected objects. |
Note: The environment variables that begin with DEBUG_
end up being
converted into an Options object that is used with %o
/%O
formatters.
See the util.inspect()
NodeJS documentation for the complete list.
If you're using this in one or more of your libraries, you should use the name of your library so that developers can toggle debugging as desired without guessing names. If you have more than one debugger you should prefix them with your library name and use :
to separate features. For example, bodyParser
from Connect
would then be connect:bodyParser
. If you append a *
to the end of your name, it will always be enabled regardless of how the DEBUG environment variable is set. You can then use it for normal output as well as debug output.
The *
character may be used as a wildcard. Suppose for example your library has
debuggers named connect:bodyParser
, connect:compress
and connect:session
.
Instead of listing all three with DEBUG=connect:bodyParser,connect:compress,connect:session
, you can simply use DEBUG=connect:*
and to run everything using this module, you can use DEBUG=*
.
You can also exclude specific debuggers by prefixing them with a -
character. For example, DEBUG=*,-connect:*
would include all debuggers except those starting with "connect:".
On Windows, the environment variable is set using the set
command.
set DEBUG=*,-not_this
Example:
set DEBUG=* & node app.js
PowerShell uses a different syntax to set environment variables.
$env:DEBUG = "*,-not_this"
For example:
$env:DEBUG='app';node app.js
Then, you can run the program to be debugged as usual.
NPM script example:
{
"scripts": {
"windowsDebug": "@powershell -Command $env:DEBUG='*';node app.js"
}
}
![]() |
![]() |
![]() |
---|---|---|
TJ Holowaychuk | Nathan Rajlich | Andrew Rhyne |
Support us with a monthly donation and help us continue our activities. [Become a backer]
Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]
(The MIT License)
Copyright (c) 2014-2019 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
README rewrite by @Richienb