Shoulder.dev Logo Shoulder.dev

debug - benhall/express-demo

Go to Shoulder Page

Debug is a tiny JavaScript debugging utility that can be used in Node.js and web browsers. It allows you to toggle debug output for different parts of your application, making it easier to identify and fix issues.

Installation

To install debug, run the following command in your terminal:

npm install debug

Usage

Once installed, you can use debug by requiring it in your JavaScript file and passing it the name of your module. This will return a decorated version of console.error that you can use to log debug statements.

const debug = require('debug')('yourModuleName');

// ... your code here ...

debug('This is a debug statement');

You can also use multiple debuggers within the same file by assigning different names to each one.

const debug = require('debug')('yourModuleName');
const debugSub = require('debug')('yourModuleName:subModule');

// ... your code here ...

debug('This is a debug statement for yourModuleName');
debugSub('This is a debug statement for yourModuleName:subModule');

To enable or disable debug output, set the DEBUG environment variable to the name of the module or modules you want to debug, separated by commas.

# To enable debug output for yourModuleName
DEBUG=yourModuleName node yourFile.js

# To disable debug output for yourModuleName
DEBUG=* node yourFile.js

Namespaces and Colors

Debug automatically generates a color for each debug instance based on its namespace name, which helps when visually parsing the debug output.

Millisecond Diff

Debug also supports showing the time difference between debug statements.

Conventions

When using debug in your libraries, use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debugger, prefix them with your library name and use “:” to separate features.

Wildcards

You can use wildcards to enable or disable multiple debuggers at once.

Environment Variables

Debug supports a few environment variables that can change the behavior of the debug logging.

Formatters

Debug uses printf-style formatting and supports several officially supported formatters. You can also add custom formatters.

Browser Support

Debug can be used in web browsers by building it using browserify or using the browserify-as-a-service build. Debug’s enable state is persisted by localStorage.

Output Streams

By default, debug logs to stderr, but you can configure it to log to stdout or other streams.

Extend

You can extend debug by creating new debug instances with extended namespaces.

Enabling and Disabling Debugging

You can enable or disable debugging dynamically using the enable() and disable() methods.

Authors and Backers

Debug is maintained by TJ Holowaychuk, Nathan Rajlich, Andrew Rhyne, and Josh Junon. You can become a backer or sponsor to support the project.

License

Debug is licensed under the MIT License.