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';
An implementation of the WHATWG Encoding Standard TextDecoder
API.
An implementation of the WHATWG Encoding Standard TextEncoder
API. Allinstances of TextEncoder
only support UTF-8 encoding.
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.
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.
The util.debuglog()
method is used to create a function that conditionallywrites debug messages to stderr
based on the existence of the NODE_DEBUG
environment 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.
The util.deprecate()
method wraps fn
(which may be a function or class) insuch a way that it is marked as deprecated.
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:
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.
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.
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.
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.
Returns true
if there is deep strict equality between val1
and val2
.Otherwise, returns false
.
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.
Stability: 1.1 - Active developmentGiven an example .env
file:
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.
Returns str
with any ANSI escape codes removed.
Stability: 1.1 - Active development
Returns the string
after replacing any surrogate code points(or equivalently, any unpaired surrogate code units) with theUnicode "replacement character" U+FFFD.
Creates and returns an AbortController
instance whose AbortSignal
is markedas transferable and can be used with structuredClone()
or postMessage()
.
Marks the given AbortSignal
as transferable so that it can be used withstructuredClone()
and postMessage()
.
Returns true
if the value is a built-in ArrayBuffer
orSharedArrayBuffer
instance.
Returns true
if the value is an arguments
object.
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.
Returns true
if the value is an instance of one of the ArrayBuffer
views, such as typedarray objects or DataView
. Equivalent toArrayBuffer.isView()
.
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.
Returns true
if the value is a BigInt64Array
instance.
Returns true
if the value is a BigUint64Array
instance.
Returns true
if the value is a boolean object, e.g. createdby new Boolean()
.
Returns true
if the value is any boxed primitive object, e.g. createdby new Boolean()
, new String()
or Object(Symbol())
.
Returns true
if value
is a CryptoKey
, false
otherwise.
Returns true
if the value is a built-in DataView
instance.
Returns true
if the value is a built-in Date
instance.
Returns true
if the value is a native External
value.
Returns true
if the value is a built-in Float32Array
instance.
Returns true
if the value is a built-in Float64Array
instance.
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.
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.
Returns true
if the value is a built-in Int16Array
instance.
Returns true
if the value is a built-in Int32Array
instance.
Returns true
if the value is a built-in Int8Array
instance.
Returns true
if value
is a KeyObject
, false
otherwise.
Returns true
if the value is a built-in Map
instance.
Returns true
if the value is an iterator returned for a built-in Map
instance.
Returns true
if the value is an instance of a Module Namespace Object.
Returns true
if the value was returned by the constructor of a built-in Error
type.
Returns true
if the value is a number object, e.g. createdby new Number()
.
Returns true
if the value is a built-in Promise
.
Returns true
if the value is a Proxy
instance.
Returns true
if the value is a regular expression object.
Returns true
if the value is a built-in Set
instance.
Returns true
if the value is an iterator returned for a built-in Set
instance.
Returns true
if the value is a string object, e.g. createdby new String()
.
Returns true
if the value is a symbol object, createdby calling Object()
on a Symbol
primitive.
Returns true
if the value is a built-in TypedArray
instance.
Returns true
if the value is a built-in Uint16Array
instance.
Returns true
if the value is a built-in Uint32Array
instance.
Returns true
if the value is a built-in Uint8Array
instance.
Returns true
if the value is a built-in Uint8ClampedArray
instance.
Returns true
if the value is a built-in WeakMap
instance.
Returns true
if the value is a built-in WeakSet
instance.
Alias for Array.isArray()
.
Returns true
if the given object
is a Boolean
. Otherwise, returns false
.
Returns true
if the given object
is a Buffer
. Otherwise, returns false
.
Returns true
if the given object
is a Date
. Otherwise, returns false
.
Returns true
if the given object
is an Error
. Otherwise, returns false
.
Returns true
if the given object
is a Function
. Otherwise, returns false
.
Returns true
if the given object
is strictly null
. Otherwise, returnsfalse
.
Returns true
if the given object
is null
or undefined
. Otherwise,returns false
.
Returns true
if the given object
is a Number
. Otherwise, returns false
.
Returns true
if the given object
is strictly an Object
and not aFunction
(even though functions are objects in JavaScript).Otherwise, returns false
.
Returns true
if the given object
is a primitive type. Otherwise, returnsfalse
.
Returns true
if the given object
is a RegExp
. Otherwise, returns false
.
Returns true
if the given object
is a string
. Otherwise, returns false
.
Returns true
if the given object
is a Symbol
. Otherwise, returns false
.
Returns true
if the given object
is undefined
. Otherwise, returns false
.
The util.log()
method prints the given string
to stdout
with an includedtimestamp.
That can be used to declare custom inspect functions.
Allows changing inspect settings from the repl.
That can be used to declare custom promisified variants of functions.