Skip to content

node:util

How Elide supports Node's util and util/types: format, inspect, promisify, parseArgs and the type predicates, and the inspect options and functions that are missing.

On this page

node:util and node:util/types provide Node’s utilities, including format, inspect, promisify, parseArgs, parseEnv, styleText, MIMEType and the types predicates; node:sys returns the same object.

Example

js
// util.mjs
import { format, inspect, isDeepStrictEqual, parseArgs, types } from "node:util";

console.log(format("%s has %d items", "cart", 3));
console.log(inspect({ tags: new Set(["a", "b"]), nested: { deep: { deeper: {} } } }, { depth: 0 }));

const { values, positionals } = parseArgs({
  args: ["--port", "8080", "-v", "app.js"],
  options: { port: { type: "string" }, verbose: { type: "boolean", short: "v" } },
  allowPositionals: true,
});
console.log(values.port, values.verbose, positionals.join());
console.log(types.isPromise(Promise.resolve()), isDeepStrictEqual({ a: [1] }, { a: [1] }));
utf-8 · js · Lines: 13

elide run util.mjs prints:

text
cart has 3 items
{ tags: [Set], nested: [Object] }
8080 true app.js
true true
utf-8 · text · Lines: 4

Differences from Node

  • inspect ignores numericSeparator and getters, and does not color regular expressions.
  • getCallSites() calls a user-defined Error.prepareStackTrace.
  • diff() is missing; toUSVString() and log() throw ERR_METHOD_NOT_IMPLEMENTED.
  • util.types predicates are non-enumerable; list them with Object.getOwnPropertyNames(util.types).

Reference

Status: Partial · 9 of 18 suite tests pass (50.0%) · since 1.1.0

Exports

ExportKindStatusNotes
format()functionFullFormats a string using printf-style substitution patterns (%s, %d, %o, etc.)
formatWithOptions()functionFullLike format(), but accepts an explicit util.inspect options object for the first argument
inspect()functionFullReturns a string representation of an object for debugging
promisify()functionFullWraps a callback-based function so it returns a Promise
inherits()functionFullSets up prototype inheritance between a constructor and a superclass
isDeepStrictEqual()functionFullReturns true if two values are deeply equal using the strict equality algorithm
deprecate()functionFullWraps a function to emit a deprecation warning when called
debuglog()functionFullReturns a logging function that writes to stderr only if the NODE_DEBUG env variable includes section
callbackify()functionFullWraps an async function (or a function that returns a Promise) so it follows the error-first callback…
getSystemErrorName()functionFullReturns the string name for a numeric error code (the errno value)
getSystemErrorMessage()functionFullReturns the string message for a numeric error code (the errno value)
getSystemErrorMap()functionFullReturns a Map of all system error codes to [name, message] pairs
toUSVString()functionFullReturns a string after replacing any lone surrogates with the Unicode replacement character U+FFFD
stripVTControlCharacters()functionFullRemoves ANSI escape codes (including color and cursor movement) from a string
styleText()functionFullFormats text with ANSI styling for terminal output
getCallSites()functionFullReturns an array of call site objects representing the current call stack
parseEnv()functionFullParses a .env file content string into a key-value object
log()functionFullWrites a formatted timestamp + message to stdout (similar to console.log but with a timestamp)
typespropertyFullObject with type-checking predicates (e.g. util.types.isDate, util.types.isMap)
TextEncoderpropertyFullThe WHATWG TextEncoder class
TextDecoderpropertyFullThe WHATWG TextDecoder class

Not implemented

convertProcessSignalToExitCode, debug, diff, isArray, setTraceSigInt, transferableAbortController, transferableAbortSignal.

Tests: node-api node:util: 9 pass, 8 fail, 1 error, 12 skipped. Failing: test-util-callbackify.js, test-util-format.js, test-util-getcallsites-preparestacktrace.js, test-util-getcallsites-sourcemap.js, test-util-inspect-getters-accessing-this.js, test-util-inspect-long-running.js, test-util-inspect-namespace.js, test-util-inspect-regexp.js, and 1 more.

Was this page helpful?