Node.js module
util
The 'node:util' module provides utility functions for debugging and supporting internal Node.js operations. It includes util.format, util.callbackify, util.promisify, and util.inspect.
These helpers simplify error handling, convert callbacks to promises, format strings, and inspect objects with configurable depth and styling.
Works in Bun · notes
Most utility functions are implemented. Missing specific functions related to call sites, system errors, and transferable AbortSignals/Controllers.
Functions 23
- util.aborted()
Listens to abort event on the provided
signaland returns a promise that resolves when thesignalis aborted. - util.callbackify()
Takes an
asyncfunction (or a function that returns aPromise) and returns a function following the error-first callback style, i.e. - util.convertProcessSignalToExitCode()
The
util.convertProcessSignalToExitCode()method converts a signal name to its corresponding POSIX exit code. - util.debuglog()
The
util.debuglog()method is used to create a function that conditionally writes debug messages tostderrbased on the existence of theNODE_DEBUGenvironment v… - util.diff()
util.diff()compares two string or array values and returns an array of difference entries. - util.format()
The
util.format()method returns a formatted string using the first argument as aprintf-like format string which can contain zero or more format specifiers. - util.formatWithOptions()
This function is identical to format, except in that it takes an
inspectOptionsargument which specifies options that are passed along to inspect. - util.getCallSites()
Returns an array of call site objects containing the stack of the caller function.
- util.getSystemErrorMap()
Returns a Map of all system error codes available from the Node.js API.
- util.getSystemErrorMessage()
Returns the string message for a numeric error code that comes from a Node.js
- util.getSystemErrorName()
Returns the string name for a numeric error code that comes from a Node.js API.
- util.inherits()
Usage of
util.inherits()is discouraged. - util.inspect()
The
util.inspect()method returns a string representation ofobjectthat is intended for debugging. - util.isDeepStrictEqual()
Returns
trueif there is deep strict equality betweenval1andval2. - util.parseArgs()
Provides a higher level API for command-line argument parsing than interacting with
process.argvdirectly. - util.parseEnv()
Stability: 1.1 - Active development
- util.promisify()
Takes a function following the common error-first callback style, i.e.
- util.setTraceSigInt()
Enable or disable printing a stack trace on
SIGINT. - util.stripVTControlCharacters()
Returns
strwith any ANSI escape codes removed. - util.styleText()
This function returns a formatted text considering the
formatpassed for printing in a terminal. - util.toUSVString()
Returns the
stringafter replacing any surrogate code points (or equivalently, any unpaired surrogate code units) with the - util.transferableAbortController()
Creates and returns an
AbortControllerinstance whoseAbortSignalis marked as transferable and can be used withstructuredClone()orpostMessage(). - util.transferableAbortSignal()
Marks the given
AbortSignalas transferable so that it can be used withstructuredClone()andpostMessage().
Classes 2
- class util.MIMEParams
The
MIMEParamsAPI provides read and write access to the parameters of aMIMEType. - class util.MIMEType
An implementation of the MIMEType class.
Interfaces 23
- interface CallSiteObject
- interface CustomPromisifyLegacy
- interface CustomPromisifySymbol
- interface DebugLogger
- interface DeprecateOptions
- interface GetCallSitesOptions
- interface Inspectable
- interface InspectColors
- interface InspectContext
- interface InspectOptions
- interface InspectStyles
- interface IsDeepStrictEqualOptions
- interface ParseArgsConfig
- interface ParseArgsOptionDescriptor
- interface ParseArgsOptionsConfig
- interface StyleTextOptions
- interface TextDecodeOptions
- interface TextDecoder
- interface TextDecoderCommon
- interface TextDecoderOptions
- interface TextEncoder
- interface TextEncoderCommon
- interface TextEncoderEncodeIntoResult