Skip to main content
util - Node documentation

Usage in Deno

import * as mod from "node:util";

The node:util module supports the needs of Node.js internal APIs. Many of the utilities are useful for application and module developers as well. To access it:

import util from 'node:util';

Classes

c
MIMEParams

The MIMEParams API provides read and write access to the parameters of a MIMEType.

c
v
TextDecoder

An implementation of the WHATWG Encoding Standard TextDecoder API.

c
v
TextEncoder

An implementation of the WHATWG Encoding Standard TextEncoder API. Allinstances of TextEncoder only support UTF-8 encoding.

Functions

f
aborted

Listens to abort event on the provided signal andreturns a promise that is fulfilled when the signal isaborted. If the passed resource is garbage collected before the signal isaborted, the returned promise shall remain pending indefinitely.

f
callbackify

Takes an async function (or a function that returns a Promise) and returns afunction following the error-first callback style, i.e. takingan (err, value) => ... callback as the last argument. In the callback, thefirst argument will be the rejection reason (or null if the Promise resolved), and the second argument will be the resolved value.

f
debuglog

The util.debuglog() method is used to create a function that conditionallywrites debug messages to stderr based on the existence of the NODE_DEBUGenvironment variable. If the section name appears within the value of thatenvironment variable, then the returned function operates similar to console.error(). If not, then the returned function is a no-op.

f
deprecate

The util.deprecate() method wraps fn (which may be a function or class) insuch a way that it is marked as deprecated.

f
format

The util.format() method returns a formatted string using the first argumentas a printf-like format string which can contain zero or more formatspecifiers. Each specifier is replaced with the converted value from thecorresponding argument. Supported specifiers are:

f
formatWithOptions

This function is identical to format, except in that it takesan inspectOptions argument which specifies options that are passed along to inspect.

f
getSystemErrorMap

Returns a Map of all system error codes available from the Node.js API.The mapping between error codes and error names is platform-dependent.See Common System Errors for the names of common errors.

f
getSystemErrorName

Returns the string name for a numeric error code that comes from a Node.js API.The mapping between error codes and error names is platform-dependent.See Common System Errors for the names of common errors.

f
inherits

Usage of util.inherits() is discouraged. Please use the ES6 class and extends keywords to get language level inheritance support. Also notethat the two styles are semantically incompatible.

f
N
inspect

The util.inspect() method returns a string representation of object that isintended for debugging. The output of util.inspect may change at any timeand should not be depended upon programmatically. Additional options may bepassed that alter the result. util.inspect() will use the constructor's name and/or @@toStringTag to makean identifiable tag for an inspected value.

f
isDeepStrictEqual

Returns true if there is deep strict equality between val1 and val2.Otherwise, returns false.

f
parseArgs

Provides a higher level API for command-line argument parsing than interactingwith process.argv directly. Takes a specification for the expected argumentsand returns a structured object with the parsed options and positionals.

f
parseEnv

Stability: 1.1 - Active developmentGiven an example .env file:

f
N
promisify

Takes a function following the common error-first callback style, i.e. takingan (err, value) => ... callback as the last argument, and returns a versionthat returns promises.

f
stripVTControlCharacters

Returns str with any ANSI escape codes removed.

f
styleText

Stability: 1.1 - Active development

f
toUSVString

Returns the string after replacing any surrogate code points(or equivalently, any unpaired surrogate code units) with theUnicode "replacement character" U+FFFD.

f
transferableAbortController

Creates and returns an AbortController instance whose AbortSignal is markedas transferable and can be used with structuredClone() or postMessage().

f
transferableAbortSignal

Marks the given AbortSignal as transferable so that it can be used withstructuredClone() and postMessage().

f
types.isAnyArrayBuffer

Returns true if the value is a built-in ArrayBuffer orSharedArrayBuffer instance.

f
types.isArgumentsObject

Returns true if the value is an arguments object.

f
types.isArrayBuffer

Returns true if the value is a built-in ArrayBuffer instance.This does not include SharedArrayBuffer instances. Usually, it isdesirable to test for both; See util.types.isAnyArrayBuffer() for that.

f
types.isArrayBufferView

Returns true if the value is an instance of one of the ArrayBuffer views, such as typedarray objects or DataView. Equivalent toArrayBuffer.isView().

f
types.isAsyncFunction

Returns true if the value is an async function.This only reports back what the JavaScript engine is seeing;in particular, the return value may not match the original source code ifa transpilation tool was used.

f
types.isBigInt64Array

Returns true if the value is a BigInt64Array instance.

f
types.isBigUint64Array

Returns true if the value is a BigUint64Array instance.

f
types.isBooleanObject

Returns true if the value is a boolean object, e.g. createdby new Boolean().

f
types.isBoxedPrimitive

Returns true if the value is any boxed primitive object, e.g. createdby new Boolean(), new String() or Object(Symbol()).

f
types.isCryptoKey

Returns true if value is a CryptoKey, false otherwise.

f
types.isDataView

Returns true if the value is a built-in DataView instance.

f
types.isDate

Returns true if the value is a built-in Date instance.

f
types.isExternal

Returns true if the value is a native External value.

f
types.isFloat32Array

Returns true if the value is a built-in Float32Array instance.

f
types.isFloat64Array

Returns true if the value is a built-in Float64Array instance.

f
types.isGeneratorFunction

Returns true if the value is a generator function.This only reports back what the JavaScript engine is seeing;in particular, the return value may not match the original source code ifa transpilation tool was used.

f
types.isGeneratorObject

Returns true if the value is a generator object as returned from abuilt-in generator function.This only reports back what the JavaScript engine is seeing;in particular, the return value may not match the original source code ifa transpilation tool was used.

f
types.isInt16Array

Returns true if the value is a built-in Int16Array instance.

f
types.isInt32Array

Returns true if the value is a built-in Int32Array instance.

f
types.isInt8Array

Returns true if the value is a built-in Int8Array instance.

f
types.isKeyObject

Returns true if value is a KeyObject, false otherwise.

f
types.isMap

Returns true if the value is a built-in Map instance.

f
types.isMapIterator

Returns true if the value is an iterator returned for a built-in Map instance.

f
types.isModuleNamespaceObject

Returns true if the value is an instance of a Module Namespace Object.

f
types.isNativeError

Returns true if the value was returned by the constructor of a built-in Error type.

f
types.isNumberObject

Returns true if the value is a number object, e.g. createdby new Number().

f
types.isPromise

Returns true if the value is a built-in Promise.

f
types.isProxy

Returns true if the value is a Proxy instance.

f
types.isRegExp

Returns true if the value is a regular expression object.

f
types.isSet

Returns true if the value is a built-in Set instance.

f
types.isSetIterator

Returns true if the value is an iterator returned for a built-in Set instance.

f
types.isSharedArrayBuffer

Returns true if the value is a built-in SharedArrayBuffer instance.This does not include ArrayBuffer instances. Usually, it isdesirable to test for both; See util.types.isAnyArrayBuffer() for that.

f
types.isStringObject

Returns true if the value is a string object, e.g. createdby new String().

f
types.isSymbolObject

Returns true if the value is a symbol object, createdby calling Object() on a Symbol primitive.

f
types.isTypedArray

Returns true if the value is a built-in TypedArray instance.

f
types.isUint16Array

Returns true if the value is a built-in Uint16Array instance.

f
types.isUint32Array

Returns true if the value is a built-in Uint32Array instance.

f
types.isUint8Array

Returns true if the value is a built-in Uint8Array instance.

f
types.isUint8ClampedArray

Returns true if the value is a built-in Uint8ClampedArray instance.

f
types.isWeakMap

Returns true if the value is a built-in WeakMap instance.

f
types.isWeakSet

Returns true if the value is a built-in WeakSet instance.

f
f
isBoolean

Returns true if the given object is a Boolean. Otherwise, returns false.

f
isBuffer

Returns true if the given object is a Buffer. Otherwise, returns false.

f
isDate

Returns true if the given object is a Date. Otherwise, returns false.

f
isError

Returns true if the given object is an Error. Otherwise, returns false.

f
isFunction

Returns true if the given object is a Function. Otherwise, returns false.

f
isNull

Returns true if the given object is strictly null. Otherwise, returnsfalse.

f
isNullOrUndefined

Returns true if the given object is null or undefined. Otherwise,returns false.

f
isNumber

Returns true if the given object is a Number. Otherwise, returns false.

f
isObject

Returns true if the given object is strictly an Objectand not aFunction (even though functions are objects in JavaScript).Otherwise, returns false.

f
isPrimitive

Returns true if the given object is a primitive type. Otherwise, returnsfalse.

f
isRegExp

Returns true if the given object is a RegExp. Otherwise, returns false.

f
isString

Returns true if the given object is a string. Otherwise, returns false.

f
isSymbol

Returns true if the given object is a Symbol. Otherwise, returns false.

f
isUndefined

Returns true if the given object is undefined. Otherwise, returns false.

f
log

The util.log() method prints the given string to stdout with an includedtimestamp.

Interfaces

I
CustomPromisifyLegacy
No documentation available
I
CustomPromisifySymbol
No documentation available
I
DebugLogger
No documentation available
I
EncodeIntoResult
No documentation available
I
InspectOptionsStylized
No documentation available
I
I
ParseArgsOptionsConfig
No documentation available

Namespaces

N
types
No documentation available

Type Aliases

T
BackgroundColors
No documentation available
T
CustomInspectFunction
No documentation available
T
CustomPromisify
No documentation available
T
DebugLoggerFunction
No documentation available
T
ExtractOptionValue
No documentation available
T
ForegroundColors
No documentation available
T
IfDefaultsFalse
No documentation available
T
IfDefaultsTrue
No documentation available
T
Modifiers
No documentation available
T
OptionToken
No documentation available
T
ParsedOptionToken
No documentation available
T
ParsedPositionals
No documentation available
T
ParsedPositionalToken
No documentation available
T
ParsedResults
No documentation available
T
ParsedTokens
No documentation available
T
ParsedValues
No documentation available
T
PreciseParsedResults
No documentation available
T
PreciseTokenForOptions
No documentation available
T
Style
No documentation available
T
Token
No documentation available
T
TokenForOptions
No documentation available

Variables

v
debug
No documentation available
v
inspect.colors
No documentation available
v
inspect.custom

That can be used to declare custom inspect functions.

v
inspect.defaultOptions
No documentation available
v
inspect.replDefaults

Allows changing inspect settings from the repl.

v
inspect.styles
No documentation available
v
promisify.custom

That can be used to declare custom promisified variants of functions.