obug Lightweight Debugging Utility
obug is a lightweight JavaScript debugging utility, forked from the popular `debug` package, specifically engineered for modern JavaScript environments. It offers comprehensive TypeScript support and native ES Module (ESM) compatibility, making it suitable for contemporary Node.js applications and browser environments (ES2015+). The current stable version is 2.1.1, with recent updates focusing on performance enhancements and customizable features. Unlike its predecessor `debug`, obug prioritizes a minimal footprint (7.7 kB package size, 1.4 KB minified + gzipped for browsers) and zero external dependencies. Key differentiators include full TypeScript type definitions, native ESM support from the ground up, and an API optimized for modern runtimes. Version 2 introduced a significant rewrite, refactoring API imports and usage for improved modularity, customization, and further reduced package size compared to v1, which aimed for `debug` compatibility but dropped older runtime support.
Common errors
-
TypeError: obug is not a function
cause Attempting to call the top-level `obug` module as a function (similar to `require('debug')('namespace')`) in an ESM context, or incorrectly importing the default export.fixUse the named export `createDebug` explicitly: `import { createDebug } from 'obug'; const debug = createDebug('my-namespace');` -
TypeError: Cannot read properties of undefined (reading 'createDebug')
cause Incorrectly trying to destructure `createDebug` from a CommonJS `require('obug')` that doesn't expose it this way, or a typo in the named import.fixEnsure you are using `import { createDebug } from 'obug';` for ESM, and verify that your environment is properly configured for ESM. If strictly in CJS, you may need a bundler to resolve the ESM module correctly. -
ReferenceError: exports is not defined in ES module scope
cause Attempting to use CommonJS-specific globals (`exports`, `module.exports`, `require`) within an ES Module file (`.mjs` or `type: "module"` in `package.json`).fixConvert your module to use ES Module syntax (`import`/`export`). `obug` is built for ESM, and mixing CJS patterns can lead to errors.
Warnings
- breaking obug v2 introduced a significant rewrite that refactors API imports and usage compared to v1. Direct replacements for `debug`'s default export might not work without modification.
- gotcha obug drops support for older browsers and Node.js versions compared to the original `debug` package, specifically targeting ES2015+ environments.
- gotcha obug is designed with native ES Module (ESM) compatibility in mind. Using CommonJS `require()` in an ESM context or a mixed environment without proper configuration can lead to import issues.
Install
-
npm install obug -
yarn add obug -
pnpm add obug
Imports
- createDebug
const createDebug = require('obug').createDebugimport { createDebug } from 'obug' - disable
const disable = require('obug').disableimport { disable } from 'obug' - namespaces
const namespaces = require('obug').namespacesimport { namespaces } from 'obug'
Quickstart
import { createDebug, disable, enable, enabled, namespaces } from 'obug';
// Enable debug messages for 'my-namespace' and its sub-namespaces
enable('my-namespace*');
// Get the currently enabled namespaces
console.log('Enabled namespaces:', namespaces());
const debug = createDebug('my-namespace', {
// All options are optional
useColors: true, // false, true, undefined for auto-detect
color: 2, // custom color (e.g., ANSI color code)
// custom formatArgs (Node.js only)
formatArgs(args) {
// Example: prepend a timestamp
args[0] = `[${new Date().toISOString()}] ${args[0]}`;
},
formatters: {}, // custom formatters (like debug.formatters)
// Node.js only options
inspectOpts: { depth: 4 },
// custom log function
log: console.log,
});
// This message will be logged because 'my-namespace' is enabled
debug('This is a debug message for %s', 'my-component');
console.log(
'Namespace:', debug.namespace, // 'my-namespace'
'Enabled:', debug.enabled, // true
'Using colors:', debug.useColors, // true
);
// Create a sub-namespace, inheriting options from the parent
const sub = debug.extend('sub-namespace');
sub('This is a sub-namespace debug message with an object: %o', { id: 123, status: 'active' });
console.log('Sub-namespace:', sub.namespace); // 'my-namespace:sub-namespace'
// Example of disabling debug messages
disable('my-namespace*');
console.log('Enabled after disable:', enabled('my-namespace')); // Should be false or empty
const debug2 = createDebug('another-namespace');
debug2('This message will NOT appear because its namespace is not enabled.');