Debug runtime values
Need a readable representation of an object while diagnosing behavior.
Node.js · Module
The node:util module provides general-purpose helpers used by Node.js internals and application developers. Its APIs cover debugging inspection, formatting, type checks, and other runtime utilities.
The module exposes general-purpose helpers for formatting, inspection, debugging, type checks, and other runtime-oriented tasks. Import it with the node:util specifier, then call the relevant utility; for example, inspect() synchronously converts a value into a debug-oriented string.
import util from 'node:util';
const util = require('node:util');| Name | Type | Description |
|---|---|---|
object | any | The value to render or examine, depending on the selected utility. |
options | Object | Optional settings that alter inspection output. |
The module provides utility-specific results; util.inspect() returns a string representation of the supplied object.
import util from 'node:util';
const value = { user: 'Ada', roles: ['admin'] };
console.log(util.inspect(value, { colors: false, depth: null }));Creates a readable debugging representation of a nested value.
Need a readable representation of an object while diagnosing behavior.
Need to identify a specific built-in object type safely.
Need shared Node.js helpers for formatting or adapting callback-based APIs.
Import the module with node:util, using either ES modules or CommonJS. The module contains utilities used by Node internals and application developers.
Follow-up: How should inspect() output be used? Only for human-oriented debugging, because its format is not a programmatic contract.
util.inspect(object, options) returns a string representation intended for debugging. Options can control aspects of formatting, such as recursion depth and color output.
Follow-up: Why should the result not be persisted as structured data? Its output may change, so it is unsuitable as a stable serialization format.
util.types supplies predicates for identifying different kinds of built-in objects. It is available from the main module or through the node:util/types subpath.
Follow-up: When is this preferable to generic checks? When code needs to distinguish specific built-in runtime types rather than merely test broad JavaScript categories.
util.format()util.inspect()util.debuglog()util.typesutil.promisify()util.callbackify()