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. All
instances of TextEncoder
only support UTF-8 encoding.
Listens to abort event on the provided signal
and
returns a promise that is fulfilled when the signal
is
aborted. If the passed resource
is garbage collected before the signal
is
aborted, the returned promise shall remain pending indefinitely.
Takes an async
function (or a function that returns a Promise
) and returns a
function following the error-first callback style, i.e. taking
an (err, value) => ...
callback as the last argument. In the callback, the
first 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 conditionally
writes debug messages to stderr
based on the existence of the NODE_DEBUG
environment variable. If the section
name appears within the value of that
environment 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) in
such a way that it is marked as deprecated.
The util.format()
method returns a formatted string using the first argument
as a printf
-like format string which can contain zero or more format
specifiers. Each specifier is replaced with the converted value from the
corresponding argument. Supported specifiers are:
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 note
that the two styles are semantically incompatible.
The util.inspect()
method returns a string representation of object
that is
intended for debugging. The output of util.inspect
may change at any time
and should not be depended upon programmatically. Additional options
may be
passed that alter the result. util.inspect()
will use the constructor's name and/or @@toStringTag
to make
an 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 interacting
with process.argv
directly. Takes a specification for the expected arguments
and returns a structured object with the parsed options and positionals.
Stability: 1.1 - Active development
Given an example .env
file:
Takes a function following the common error-first callback style, i.e. taking
an (err, value) => ...
callback as the last argument, and returns a version
that 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 the
Unicode "replacement character" U+FFFD.
Returns true
if the value is a built-in ArrayBuffer
or
SharedArrayBuffer
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 is
desirable 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 typed
array objects or DataView
. Equivalent to
ArrayBuffer.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 if
a 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. created
by new Boolean()
.
Returns true
if the value is any boxed primitive object, e.g. created
by 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 if
a transpilation tool was used.
Returns true
if the value is a generator object as returned from a
built-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 if
a 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. created
by 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. created
by new String()
.
Returns true
if the value is a symbol object, created
by 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 included
timestamp.
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.