node:util API Reference

Formatting, inspection, callbacks, argument helpers, and type predicates

Common helpers available in node:util include:

  • format, formatWithOptions, inspect, and stripVTControlCharacters;
  • promisify, callbackify, inherits, deprecate, and debuglog;
  • isDeepStrictEqual, parseArgs, parseEnv, and styleText;
  • getSystemErrorName, getSystemErrorMessage, and getSystemErrorMap;
  • getCallSites;
  • the global TextEncoder and TextDecoder constructors.

toUSVString() and the legacy log() helper are present but throw an implementation error.

Examples

const util = require("node:util");

util.inspect({ a: [1, 2] }); // "{ a: [ 1, 2 ] }"
util.format("%s:%d", "port", 8080); // "port:8080"
util.isDeepStrictEqual({ a: 1 }, { a: 1 }); // true

inspect() handles common containers, typed arrays, buffers, functions, circular references, promises, and util.inspect.custom. Options including depth, breakLength, compact, and maxArrayLength are available.

promisify() honors util.promisify.custom.

util.types

The predicate family covers common arrays and views, dates, maps, sets, errors, promises, proxies, regular expressions, weak collections, boxed primitives, function kinds, iterators, and typed-array specializations. Not every Node predicate is present; test for newer predicates such as isCryptoKey before calling them.

See also