Debug Logger
The `debug` package is a lightweight and highly popular debugging utility designed for both Node.js and web browser environments. Currently stable at version `4.4.3`, it maintains a regular release cadence with frequent patch and minor updates addressing bug fixes and minor enhancements. Its core functionality revolves around providing a simple, namespace-based logging mechanism that allows developers to selectively enable or disable debug output for different parts of an application using the `DEBUG` environment variable (or `localStorage` in browsers). Key differentiators include its minimal footprint, automatic colorized output in supported terminals and browser developer tools, and the useful millisecond difference displayed between consecutive log calls, aiding in performance analysis. The project recently migrated its repository from `visionmedia/debug` to `debug-js/debug`. It is crucial to note that version `4.4.2` was compromised and users should upgrade immediately to `4.4.3` or later to avoid potential security risks.
Common errors
-
Debug messages are not appearing in the console.
cause The `DEBUG` environment variable is not set or is set incorrectly for your shell.fixEnsure the `DEBUG` environment variable is correctly set before running your application. For example, `DEBUG='my-app:*' node app.js` (Linux/macOS), `set DEBUG=my-app:* & node app.js` (Windows CMD), or `$env:DEBUG='my-app:*'; node app.js` (Windows PowerShell). -
TypeError: debug is not a function
cause You are trying to call the `debug` module directly without first creating a debugger instance by providing a namespace string.fixAlways call `require('debug')` or `import debug from 'debug'` with a namespace string. Correct: `const myDebug = require('debug')('my-namespace'); myDebug('hello');` -
Colors are not showing in debug output in Node.js.
cause The `supports-color` package is not installed, or your terminal emulator does not support colors (is not a TTY).fixInstall `supports-color` (`npm install supports-color`) and ensure your Node.js application is running in an environment where `process.stderr` is a TTY (e.g., directly in a terminal, not piped to a file).
Warnings
- breaking Version `4.4.2` was compromised and contains malicious code. It is critical to avoid this version and upgrade immediately.
- breaking The `.enable()` API no longer supports regular expressions as input. Providing regex patterns will not work as expected and may lead to inefficient warnings.
- gotcha For optimal colorized output in Node.js, the optional `supports-color` package should be installed alongside `debug`. Without it, `debug` will only use a limited set of basic colors.
- gotcha Setting the `DEBUG` environment variable differs across Windows command prompts (CMD vs PowerShell) and Linux/macOS shells. Incorrect syntax will result in debug messages not appearing.
- deprecated Older versions of `debug` used `String.prototype.substr()` and `RegExp.$1`, which are deprecated APIs. While `debug` has updated internally, ensure your own code avoids these where possible.
Install
-
npm install debug -
yarn add debug -
pnpm add debug
Imports
- debug
import { debug } from 'debug';import debug from 'debug';
- debug factory
const debug = require('debug'); debug('my-namespace', 'hello');const createDebug = require('debug'); const debug = createDebug('my-namespace'); - debug.enable
debug.enable(/my-app:.*/);
import debug from 'debug'; debug.enable('my-app:*');
Quickstart
import createDebug from 'debug';
const appDebug = createDebug('my-app:main');
const workerADebug = createDebug('my-app:worker:a');
const workerBDebug = createDebug('my-app:worker:b');
// --- app.ts ---
appDebug('Booting up application.');
// Simulate an HTTP server setup
setTimeout(() => {
appDebug('Server listening on port 3000');
// Simulate a request
appDebug('GET /users');
}, 100);
// --- worker.ts ---
function workA() {
workerADebug('Doing lots of uninteresting work...');
setTimeout(workA, Math.random() * 500);
}
function workB() {
workerBDebug('Doing some specific work...');
setTimeout(workB, Math.random() * 1000);
}
workA();
workB();
// To run this example:
// 1. Save as `example.ts`
// 2. Transpile (e.g., `tsc example.ts`)
// 3. Run with `DEBUG='my-app:*' node example.js`
// On Windows PowerShell: `$env:DEBUG='my-app:*'; node example.js`