Debug Logger

4.4.3 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates creating multiple debugger instances with different namespaces and enabling them via the `DEBUG` environment variable.

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`

view raw JSON →