{ "type": "module", "source": "doc/api/util.md", "modules": [ { "textRaw": "Util", "name": "util", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "
Source Code: lib/util.js
\nThe util
module supports the needs of Node.js internal APIs. Many of the\nutilities are useful for application and module developers as well. To access\nit:
const util = require('util');\n
",
"methods": [
{
"textRaw": "`util.callbackify(original)`",
"type": "method",
"name": "callbackify",
"meta": {
"added": [
"v8.2.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {Function} a callback style function",
"name": "return",
"type": "Function",
"desc": "a callback style function"
},
"params": [
{
"textRaw": "`original` {Function} An `async` function",
"name": "original",
"type": "Function",
"desc": "An `async` function"
}
]
}
],
"desc": "Takes an async
function (or a function that returns a Promise
) and returns a\nfunction following the error-first callback style, i.e. taking\nan (err, value) => ...
callback as the last argument. In the callback, the\nfirst argument will be the rejection reason (or null
if the Promise
\nresolved), and the second argument will be the resolved value.
const util = require('util');\n\nasync function fn() {\n return 'hello world';\n}\nconst callbackFunction = util.callbackify(fn);\n\ncallbackFunction((err, ret) => {\n if (err) throw err;\n console.log(ret);\n});\n
\nWill print:
\nhello world\n
\nThe callback is executed asynchronously, and will have a limited stack trace.\nIf the callback throws, the process will emit an 'uncaughtException'
\nevent, and if not handled will exit.
Since null
has a special meaning as the first argument to a callback, if a\nwrapped function rejects a Promise
with a falsy value as a reason, the value\nis wrapped in an Error
with the original value stored in a field named\nreason
.
function fn() {\n return Promise.reject(null);\n}\nconst callbackFunction = util.callbackify(fn);\n\ncallbackFunction((err, ret) => {\n // When the Promise was rejected with `null` it is wrapped with an Error and\n // the original value is stored in `reason`.\n err && err.hasOwnProperty('reason') && err.reason === null; // true\n});\n
"
},
{
"textRaw": "`util.debuglog(section[, callback])`",
"type": "method",
"name": "debuglog",
"meta": {
"added": [
"v0.11.3"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {Function} The logging function",
"name": "return",
"type": "Function",
"desc": "The logging function"
},
"params": [
{
"textRaw": "`section` {string} A string identifying the portion of the application for which the `debuglog` function is being created.",
"name": "section",
"type": "string",
"desc": "A string identifying the portion of the application for which the `debuglog` function is being created."
},
{
"textRaw": "`callback` {Function} A callback invoked the first time the logging function is called with a function argument that is a more optimized logging function.",
"name": "callback",
"type": "Function",
"desc": "A callback invoked the first time the logging function is called with a function argument that is a more optimized logging function."
}
]
}
],
"desc": "The util.debuglog()
method is used to create a function that conditionally\nwrites debug messages to stderr
based on the existence of the NODE_DEBUG
\nenvironment variable. If the section
name appears within the value of that\nenvironment variable, then the returned function operates similar to\nconsole.error()
. If not, then the returned function is a no-op.
const util = require('util');\nconst debuglog = util.debuglog('foo');\n\ndebuglog('hello from foo [%d]', 123);\n
\nIf this program is run with NODE_DEBUG=foo
in the environment, then\nit will output something like:
FOO 3245: hello from foo [123]\n
\nwhere 3245
is the process id. If it is not run with that\nenvironment variable set, then it will not print anything.
The section
supports wildcard also:
const util = require('util');\nconst debuglog = util.debuglog('foo-bar');\n\ndebuglog('hi there, it\\'s foo-bar [%d]', 2333);\n
\nif it is run with NODE_DEBUG=foo*
in the environment, then it will output\nsomething like:
FOO-BAR 3257: hi there, it's foo-bar [2333]\n
\nMultiple comma-separated section
names may be specified in the NODE_DEBUG
\nenvironment variable: NODE_DEBUG=fs,net,tls
.
The optional callback
argument can be used to replace the logging function\nwith a different function that doesn't have any initialization or\nunnecessary wrapping.
const util = require('util');\nlet debuglog = util.debuglog('internals', (debug) => {\n // Replace with a logging function that optimizes out\n // testing if the section is enabled\n debuglog = debug;\n});\n
",
"modules": [
{
"textRaw": "`debuglog().enabled`",
"name": "`debuglog().enabled`",
"meta": {
"added": [
"v14.9.0"
],
"changes": []
},
"desc": "The util.debuglog().enabled
getter is used to create a test that can be used\nin conditionals based on the existence of the NODE_DEBUG
environment variable.\nIf the section
name appears within the value of that environment variable,\nthen the returned value will be true
. If not, then the returned value will be\nfalse
.
const util = require('util');\nconst enabled = util.debuglog('foo').enabled;\nif (enabled) {\n console.log('hello from foo [%d]', 123);\n}\n
\nIf this program is run with NODE_DEBUG=foo
in the environment, then it will\noutput something like:
hello from foo [123]\n
",
"type": "module",
"displayName": "`debuglog().enabled`"
}
]
},
{
"textRaw": "`util.debug(section)`",
"type": "method",
"name": "debug",
"meta": {
"added": [
"v14.9.0"
],
"changes": []
},
"signatures": [
{
"params": []
}
],
"desc": "Alias for util.debuglog
. Usage allows for readability of that doesn't imply\nlogging when only using util.debuglog().enabled
.
The util.deprecate()
method wraps fn
(which may be a function or class) in\nsuch a way that it is marked as deprecated.
const util = require('util');\n\nexports.obsoleteFunction = util.deprecate(() => {\n // Do something here.\n}, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');\n
\nWhen called, util.deprecate()
will return a function that will emit a\nDeprecationWarning
using the 'warning'
event. The warning will\nbe emitted and printed to stderr
the first time the returned function is\ncalled. After the warning is emitted, the wrapped function is called without\nemitting a warning.
If the same optional code
is supplied in multiple calls to util.deprecate()
,\nthe warning will be emitted only once for that code
.
const util = require('util');\n\nconst fn1 = util.deprecate(someFunction, someMessage, 'DEP0001');\nconst fn2 = util.deprecate(someOtherFunction, someOtherMessage, 'DEP0001');\nfn1(); // Emits a deprecation warning with code DEP0001\nfn2(); // Does not emit a deprecation warning because it has the same code\n
\nIf either the --no-deprecation
or --no-warnings
command-line flags are\nused, or if the process.noDeprecation
property is set to true
prior to\nthe first deprecation warning, the util.deprecate()
method does nothing.
If the --trace-deprecation
or --trace-warnings
command-line flags are set,\nor the process.traceDeprecation
property is set to true
, a warning and a\nstack trace are printed to stderr
the first time the deprecated function is\ncalled.
If the --throw-deprecation
command-line flag is set, or the\nprocess.throwDeprecation
property is set to true
, then an exception will be\nthrown when the deprecated function is called.
The --throw-deprecation
command-line flag and process.throwDeprecation
\nproperty take precedence over --trace-deprecation
and\nprocess.traceDeprecation
.
The util.format()
method returns a formatted string using the first argument\nas a printf
-like format string which can contain zero or more format\nspecifiers. Each specifier is replaced with the converted value from the\ncorresponding argument. Supported specifiers are:
%s
: String
will be used to convert all values except BigInt
, Object
\nand -0
. BigInt
values will be represented with an n
and Objects that\nhave no user defined toString
function are inspected using util.inspect()
\nwith options { depth: 0, colors: false, compact: 3 }
.%d
: Number
will be used to convert all values except BigInt
and\nSymbol
.%i
: parseInt(value, 10)
is used for all values except BigInt
and\nSymbol
.%f
: parseFloat(value)
is used for all values expect Symbol
.%j
: JSON. Replaced with the string '[Circular]'
if the argument contains\ncircular references.%o
: Object
. A string representation of an object with generic JavaScript\nobject formatting. Similar to util.inspect()
with options\n{ showHidden: true, showProxy: true }
. This will show the full object\nincluding non-enumerable properties and proxies.%O
: Object
. A string representation of an object with generic JavaScript\nobject formatting. Similar to util.inspect()
without options. This will show\nthe full object not including non-enumerable properties and proxies.%c
: CSS
. This specifier is ignored and will skip any CSS passed in.%%
: single percent sign ('%'
). This does not consume an argument.If a specifier does not have a corresponding argument, it is not replaced:
\nutil.format('%s:%s', 'foo');\n// Returns: 'foo:%s'\n
\nValues that are not part of the format string are formatted using\nutil.inspect()
if their type is not string
.
If there are more arguments passed to the util.format()
method than the\nnumber of specifiers, the extra arguments are concatenated to the returned\nstring, separated by spaces:
util.format('%s:%s', 'foo', 'bar', 'baz');\n// Returns: 'foo:bar baz'\n
\nIf the first argument does not contain a valid format specifier, util.format()
\nreturns a string that is the concatenation of all arguments separated by spaces:
util.format(1, 2, 3);\n// Returns: '1 2 3'\n
\nIf only one argument is passed to util.format()
, it is returned as it is\nwithout any formatting:
util.format('%% %s');\n// Returns: '%% %s'\n
\nutil.format()
is a synchronous method that is intended as a debugging tool.\nSome input values can have a significant performance overhead that can block the\nevent loop. Use this function with care and never in a hot code path.
This function is identical to util.format()
, except in that it takes\nan inspectOptions
argument which specifies options that are passed along to\nutil.inspect()
.
util.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 });\n// Returns 'See object { foo: 42 }', where `42` is colored as a number\n// when printed to a terminal.\n
"
},
{
"textRaw": "`util.getSystemErrorName(err)`",
"type": "method",
"name": "getSystemErrorName",
"meta": {
"added": [
"v9.7.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {string}",
"name": "return",
"type": "string"
},
"params": [
{
"textRaw": "`err` {number}",
"name": "err",
"type": "number"
}
]
}
],
"desc": "Returns the string name for a numeric error code that comes from a Node.js API.\nThe mapping between error codes and error names is platform-dependent.\nSee Common System Errors for the names of common errors.
\nfs.access('file/that/does/not/exist', (err) => {\n const name = util.getSystemErrorName(err.errno);\n console.error(name); // ENOENT\n});\n
"
},
{
"textRaw": "`util.inherits(constructor, superConstructor)`",
"type": "method",
"name": "inherits",
"meta": {
"added": [
"v0.3.0"
],
"changes": [
{
"version": "v5.0.0",
"pr-url": "https://github.com/nodejs/node/pull/3455",
"description": "The `constructor` parameter can refer to an ES6 class now."
}
]
},
"signatures": [
{
"params": [
{
"textRaw": "`constructor` {Function}",
"name": "constructor",
"type": "Function"
},
{
"textRaw": "`superConstructor` {Function}",
"name": "superConstructor",
"type": "Function"
}
]
}
],
"desc": "Usage of util.inherits()
is discouraged. Please use the ES6 class
and\nextends
keywords to get language level inheritance support. Also note\nthat the two styles are semantically incompatible.
Inherit the prototype methods from one constructor into another. The\nprototype of constructor
will be set to a new object created from\nsuperConstructor
.
This mainly adds some input validation on top of\nObject.setPrototypeOf(constructor.prototype, superConstructor.prototype)
.\nAs an additional convenience, superConstructor
will be accessible\nthrough the constructor.super_
property.
const util = require('util');\nconst EventEmitter = require('events');\n\nfunction MyStream() {\n EventEmitter.call(this);\n}\n\nutil.inherits(MyStream, EventEmitter);\n\nMyStream.prototype.write = function(data) {\n this.emit('data', data);\n};\n\nconst stream = new MyStream();\n\nconsole.log(stream instanceof EventEmitter); // true\nconsole.log(MyStream.super_ === EventEmitter); // true\n\nstream.on('data', (data) => {\n console.log(`Received data: \"${data}\"`);\n});\nstream.write('It works!'); // Received data: \"It works!\"\n
\nES6 example using class
and extends
:
const EventEmitter = require('events');\n\nclass MyStream extends EventEmitter {\n write(data) {\n this.emit('data', data);\n }\n}\n\nconst stream = new MyStream();\n\nstream.on('data', (data) => {\n console.log(`Received data: \"${data}\"`);\n});\nstream.write('With ES6');\n
"
},
{
"textRaw": "`util.inspect(object[, options])`",
"type": "method",
"name": "inspect",
"meta": {
"added": [
"v0.3.0"
],
"changes": [
{
"version": [
"v14.6.0",
"v12.19.0"
],
"pr-url": "https://github.com/nodejs/node/pull/33690",
"description": "If `object` is from a different `vm.Context` now, a custom inspection function on it will not receive context-specific arguments anymore."
},
{
"version": [
"v13.13.0",
"v12.17.0"
],
"pr-url": "https://github.com/nodejs/node/pull/32392",
"description": "The `maxStringLength` option is supported now."
},
{
"version": [
"v13.5.0",
"v12.16.0"
],
"pr-url": "https://github.com/nodejs/node/pull/30768",
"description": "User defined prototype properties are inspected in case `showHidden` is `true`."
},
{
"version": "v13.0.0",
"pr-url": "https://github.com/nodejs/node/pull/27685",
"description": "Circular references now include a marker to the reference."
},
{
"version": "v12.0.0",
"pr-url": "https://github.com/nodejs/node/pull/27109",
"description": "The `compact` options default is changed to `3` and the `breakLength` options default is changed to `80`."
},
{
"version": "v12.0.0",
"pr-url": "https://github.com/nodejs/node/pull/24971",
"description": "Internal properties no longer appear in the context argument of a custom inspection function."
},
{
"version": "v11.11.0",
"pr-url": "https://github.com/nodejs/node/pull/26269",
"description": "The `compact` option accepts numbers for a new output mode."
},
{
"version": "v11.7.0",
"pr-url": "https://github.com/nodejs/node/pull/25006",
"description": "ArrayBuffers now also show their binary contents."
},
{
"version": "v11.5.0",
"pr-url": "https://github.com/nodejs/node/pull/24852",
"description": "The `getters` option is supported now."
},
{
"version": "v11.4.0",
"pr-url": "https://github.com/nodejs/node/pull/24326",
"description": "The `depth` default changed back to `2`."
},
{
"version": "v11.0.0",
"pr-url": "https://github.com/nodejs/node/pull/22846",
"description": "The `depth` default changed to `20`."
},
{
"version": "v11.0.0",
"pr-url": "https://github.com/nodejs/node/pull/22756",
"description": "The inspection output is now limited to about 128 MB. Data above that size will not be fully inspected."
},
{
"version": "v10.12.0",
"pr-url": "https://github.com/nodejs/node/pull/22788",
"description": "The `sorted` option is supported now."
},
{
"version": "v10.6.0",
"pr-url": "https://github.com/nodejs/node/pull/20725",
"description": "Inspecting linked lists and similar objects is now possible up to the maximum call stack size."
},
{
"version": "v10.0.0",
"pr-url": "https://github.com/nodejs/node/pull/19259",
"description": "The `WeakMap` and `WeakSet` entries can now be inspected as well."
},
{
"version": "v9.9.0",
"pr-url": "https://github.com/nodejs/node/pull/17576",
"description": "The `compact` option is supported now."
},
{
"version": "v6.6.0",
"pr-url": "https://github.com/nodejs/node/pull/8174",
"description": "Custom inspection functions can now return `this`."
},
{
"version": "v6.3.0",
"pr-url": "https://github.com/nodejs/node/pull/7499",
"description": "The `breakLength` option is supported now."
},
{
"version": "v6.1.0",
"pr-url": "https://github.com/nodejs/node/pull/6334",
"description": "The `maxArrayLength` option is supported now; in particular, long arrays are truncated by default."
},
{
"version": "v6.1.0",
"pr-url": "https://github.com/nodejs/node/pull/6465",
"description": "The `showProxy` option is supported now."
}
]
},
"signatures": [
{
"return": {
"textRaw": "Returns: {string} The representation of `object`.",
"name": "return",
"type": "string",
"desc": "The representation of `object`."
},
"params": [
{
"textRaw": "`object` {any} Any JavaScript primitive or `Object`.",
"name": "object",
"type": "any",
"desc": "Any JavaScript primitive or `Object`."
},
{
"textRaw": "`options` {Object}",
"name": "options",
"type": "Object",
"options": [
{
"textRaw": "`showHidden` {boolean} If `true`, `object`'s non-enumerable symbols and properties are included in the formatted result. [`WeakMap`][] and [`WeakSet`][] entries are also included as well as user defined prototype properties (excluding method properties). **Default:** `false`.",
"name": "showHidden",
"type": "boolean",
"default": "`false`",
"desc": "If `true`, `object`'s non-enumerable symbols and properties are included in the formatted result. [`WeakMap`][] and [`WeakSet`][] entries are also included as well as user defined prototype properties (excluding method properties)."
},
{
"textRaw": "`depth` {number} Specifies the number of times to recurse while formatting `object`. This is useful for inspecting large objects. To recurse up to the maximum call stack size pass `Infinity` or `null`. **Default:** `2`.",
"name": "depth",
"type": "number",
"default": "`2`",
"desc": "Specifies the number of times to recurse while formatting `object`. This is useful for inspecting large objects. To recurse up to the maximum call stack size pass `Infinity` or `null`."
},
{
"textRaw": "`colors` {boolean} If `true`, the output is styled with ANSI color codes. Colors are customizable. See [Customizing `util.inspect` colors][]. **Default:** `false`.",
"name": "colors",
"type": "boolean",
"default": "`false`",
"desc": "If `true`, the output is styled with ANSI color codes. Colors are customizable. See [Customizing `util.inspect` colors][]."
},
{
"textRaw": "`customInspect` {boolean} If `false`, `[util.inspect.custom](depth, opts)` functions are not invoked. **Default:** `true`.",
"name": "customInspect",
"type": "boolean",
"default": "`true`",
"desc": "If `false`, `[util.inspect.custom](depth, opts)` functions are not invoked."
},
{
"textRaw": "`showProxy` {boolean} If `true`, `Proxy` inspection includes the [`target` and `handler`][] objects. **Default:** `false`.",
"name": "showProxy",
"type": "boolean",
"default": "`false`",
"desc": "If `true`, `Proxy` inspection includes the [`target` and `handler`][] objects."
},
{
"textRaw": "`maxArrayLength` {integer} Specifies the maximum number of `Array`, [`TypedArray`][], [`WeakMap`][] and [`WeakSet`][] elements to include when formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or negative to show no elements. **Default:** `100`.",
"name": "maxArrayLength",
"type": "integer",
"default": "`100`",
"desc": "Specifies the maximum number of `Array`, [`TypedArray`][], [`WeakMap`][] and [`WeakSet`][] elements to include when formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or negative to show no elements."
},
{
"textRaw": "`maxStringLength` {integer} Specifies the maximum number of characters to include when formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or negative to show no characters. **Default:** `10000`.",
"name": "maxStringLength",
"type": "integer",
"default": "`10000`",
"desc": "Specifies the maximum number of characters to include when formatting. Set to `null` or `Infinity` to show all elements. Set to `0` or negative to show no characters."
},
{
"textRaw": "`breakLength` {integer} The length at which input values are split across multiple lines. Set to `Infinity` to format the input as a single line (in combination with `compact` set to `true` or any number >= `1`). **Default:** `80`.",
"name": "breakLength",
"type": "integer",
"default": "`80`",
"desc": "The length at which input values are split across multiple lines. Set to `Infinity` to format the input as a single line (in combination with `compact` set to `true` or any number >= `1`)."
},
{
"textRaw": "`compact` {boolean|integer} Setting this to `false` causes each object key to be displayed on a new line. It will also add new lines to text that is longer than `breakLength`. If set to a number, the most `n` inner elements are united on a single line as long as all properties fit into `breakLength`. Short array elements are also grouped together. No text will be reduced below 16 characters, no matter the `breakLength` size. For more information, see the example below. **Default:** `3`.",
"name": "compact",
"type": "boolean|integer",
"default": "`3`",
"desc": "Setting this to `false` causes each object key to be displayed on a new line. It will also add new lines to text that is longer than `breakLength`. If set to a number, the most `n` inner elements are united on a single line as long as all properties fit into `breakLength`. Short array elements are also grouped together. No text will be reduced below 16 characters, no matter the `breakLength` size. For more information, see the example below."
},
{
"textRaw": "`sorted` {boolean|Function} If set to `true` or a function, all properties of an object, and `Set` and `Map` entries are sorted in the resulting string. If set to `true` the [default sort][] is used. If set to a function, it is used as a [compare function][].",
"name": "sorted",
"type": "boolean|Function",
"desc": "If set to `true` or a function, all properties of an object, and `Set` and `Map` entries are sorted in the resulting string. If set to `true` the [default sort][] is used. If set to a function, it is used as a [compare function][]."
},
{
"textRaw": "`getters` {boolean|string} If set to `true`, getters are inspected. If set to `'get'`, only getters without a corresponding setter are inspected. If set to `'set'`, only getters with a corresponding setter are inspected. This might cause side effects depending on the getter function. **Default:** `false`.",
"name": "getters",
"type": "boolean|string",
"default": "`false`",
"desc": "If set to `true`, getters are inspected. If set to `'get'`, only getters without a corresponding setter are inspected. If set to `'set'`, only getters with a corresponding setter are inspected. This might cause side effects depending on the getter function."
}
]
}
]
}
],
"desc": "The util.inspect()
method returns a string representation of object
that is\nintended for debugging. The output of util.inspect
may change at any time\nand should not be depended upon programmatically. Additional options
may be\npassed that alter the result.\nutil.inspect()
will use the constructor's name and/or @@toStringTag
to make\nan identifiable tag for an inspected value.
class Foo {\n get [Symbol.toStringTag]() {\n return 'bar';\n }\n}\n\nclass Bar {}\n\nconst baz = Object.create(null, { [Symbol.toStringTag]: { value: 'foo' } });\n\nutil.inspect(new Foo()); // 'Foo [bar] {}'\nutil.inspect(new Bar()); // 'Bar {}'\nutil.inspect(baz); // '[foo] {}'\n
\nCircular references point to their anchor by using a reference index:
\nconst { inspect } = require('util');\n\nconst obj = {};\nobj.a = [obj];\nobj.b = {};\nobj.b.inner = obj.b;\nobj.b.obj = obj;\n\nconsole.log(inspect(obj));\n// <ref *1> {\n// a: [ [Circular *1] ],\n// b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }\n// }\n
\nThe following example inspects all properties of the util
object:
const util = require('util');\n\nconsole.log(util.inspect(util, { showHidden: true, depth: null }));\n
\nThe following example highlights the effect of the compact
option:
const util = require('util');\n\nconst o = {\n a: [1, 2, [[\n 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ' +\n 'eiusmod tempor incididunt ut labore et dolore magna aliqua.',\n 'test',\n 'foo']], 4],\n b: new Map([['za', 1], ['zb', 'test']])\n};\nconsole.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));\n\n// { a:\n// [ 1,\n// 2,\n// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long line\n// 'test',\n// 'foo' ] ],\n// 4 ],\n// b: Map(2) { 'za' => 1, 'zb' => 'test' } }\n\n// Setting `compact` to false changes the output to be more reader friendly.\nconsole.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));\n\n// {\n// a: [\n// 1,\n// 2,\n// [\n// [\n// 'Lorem ipsum dolor sit amet, consectetur ' +\n// 'adipiscing elit, sed do eiusmod tempor ' +\n// 'incididunt ut labore et dolore magna ' +\n// 'aliqua.,\n// 'test',\n// 'foo'\n// ]\n// ],\n// 4\n// ],\n// b: Map(2) {\n// 'za' => 1,\n// 'zb' => 'test'\n// }\n// }\n\n// Setting `breakLength` to e.g. 150 will print the \"Lorem ipsum\" text in a\n// single line.\n// Reducing the `breakLength` will split the \"Lorem ipsum\" text in smaller\n// chunks.\n
\nThe showHidden
option allows WeakMap
and WeakSet
entries to be\ninspected. If there are more entries than maxArrayLength
, there is no\nguarantee which entries are displayed. That means retrieving the same\nWeakSet
entries twice may result in different output. Furthermore, entries\nwith no remaining strong references may be garbage collected at any time.
const { inspect } = require('util');\n\nconst obj = { a: 1 };\nconst obj2 = { b: 2 };\nconst weakSet = new WeakSet([obj, obj2]);\n\nconsole.log(inspect(weakSet, { showHidden: true }));\n// WeakSet { { a: 1 }, { b: 2 } }\n
\nThe sorted
option ensures that an object's property insertion order does not\nimpact the result of util.inspect()
.
const { inspect } = require('util');\nconst assert = require('assert');\n\nconst o1 = {\n b: [2, 3, 1],\n a: '`a` comes before `b`',\n c: new Set([2, 3, 1])\n};\nconsole.log(inspect(o1, { sorted: true }));\n// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }\nconsole.log(inspect(o1, { sorted: (a, b) => b.localeCompare(a) }));\n// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }\n\nconst o2 = {\n c: new Set([2, 1, 3]),\n a: '`a` comes before `b`',\n b: [2, 3, 1]\n};\nassert.strict.equal(\n inspect(o1, { sorted: true }),\n inspect(o2, { sorted: true })\n);\n
\nutil.inspect()
is a synchronous method intended for debugging. Its maximum\noutput length is approximately 128 MB. Inputs that result in longer output will\nbe truncated.
The util.inspect()
method returns a string representation of object
that is\nintended for debugging. The output of util.inspect
may change at any time\nand should not be depended upon programmatically. Additional options
may be\npassed that alter the result.\nutil.inspect()
will use the constructor's name and/or @@toStringTag
to make\nan identifiable tag for an inspected value.
class Foo {\n get [Symbol.toStringTag]() {\n return 'bar';\n }\n}\n\nclass Bar {}\n\nconst baz = Object.create(null, { [Symbol.toStringTag]: { value: 'foo' } });\n\nutil.inspect(new Foo()); // 'Foo [bar] {}'\nutil.inspect(new Bar()); // 'Bar {}'\nutil.inspect(baz); // '[foo] {}'\n
\nCircular references point to their anchor by using a reference index:
\nconst { inspect } = require('util');\n\nconst obj = {};\nobj.a = [obj];\nobj.b = {};\nobj.b.inner = obj.b;\nobj.b.obj = obj;\n\nconsole.log(inspect(obj));\n// <ref *1> {\n// a: [ [Circular *1] ],\n// b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }\n// }\n
\nThe following example inspects all properties of the util
object:
const util = require('util');\n\nconsole.log(util.inspect(util, { showHidden: true, depth: null }));\n
\nThe following example highlights the effect of the compact
option:
const util = require('util');\n\nconst o = {\n a: [1, 2, [[\n 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ' +\n 'eiusmod tempor incididunt ut labore et dolore magna aliqua.',\n 'test',\n 'foo']], 4],\n b: new Map([['za', 1], ['zb', 'test']])\n};\nconsole.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));\n\n// { a:\n// [ 1,\n// 2,\n// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long line\n// 'test',\n// 'foo' ] ],\n// 4 ],\n// b: Map(2) { 'za' => 1, 'zb' => 'test' } }\n\n// Setting `compact` to false changes the output to be more reader friendly.\nconsole.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));\n\n// {\n// a: [\n// 1,\n// 2,\n// [\n// [\n// 'Lorem ipsum dolor sit amet, consectetur ' +\n// 'adipiscing elit, sed do eiusmod tempor ' +\n// 'incididunt ut labore et dolore magna ' +\n// 'aliqua.,\n// 'test',\n// 'foo'\n// ]\n// ],\n// 4\n// ],\n// b: Map(2) {\n// 'za' => 1,\n// 'zb' => 'test'\n// }\n// }\n\n// Setting `breakLength` to e.g. 150 will print the \"Lorem ipsum\" text in a\n// single line.\n// Reducing the `breakLength` will split the \"Lorem ipsum\" text in smaller\n// chunks.\n
\nThe showHidden
option allows WeakMap
and WeakSet
entries to be\ninspected. If there are more entries than maxArrayLength
, there is no\nguarantee which entries are displayed. That means retrieving the same\nWeakSet
entries twice may result in different output. Furthermore, entries\nwith no remaining strong references may be garbage collected at any time.
const { inspect } = require('util');\n\nconst obj = { a: 1 };\nconst obj2 = { b: 2 };\nconst weakSet = new WeakSet([obj, obj2]);\n\nconsole.log(inspect(weakSet, { showHidden: true }));\n// WeakSet { { a: 1 }, { b: 2 } }\n
\nThe sorted
option ensures that an object's property insertion order does not\nimpact the result of util.inspect()
.
const { inspect } = require('util');\nconst assert = require('assert');\n\nconst o1 = {\n b: [2, 3, 1],\n a: '`a` comes before `b`',\n c: new Set([2, 3, 1])\n};\nconsole.log(inspect(o1, { sorted: true }));\n// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }\nconsole.log(inspect(o1, { sorted: (a, b) => b.localeCompare(a) }));\n// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }\n\nconst o2 = {\n c: new Set([2, 1, 3]),\n a: '`a` comes before `b`',\n b: [2, 3, 1]\n};\nassert.strict.equal(\n inspect(o1, { sorted: true }),\n inspect(o2, { sorted: true })\n);\n
\nutil.inspect()
is a synchronous method intended for debugging. Its maximum\noutput length is approximately 128 MB. Inputs that result in longer output will\nbe truncated.
Color output (if enabled) of util.inspect
is customizable globally\nvia the util.inspect.styles
and util.inspect.colors
properties.
util.inspect.styles
is a map associating a style name to a color from\nutil.inspect.colors
.
The default styles and associated colors are:
\nbigint
: yellow
boolean
: yellow
date
: magenta
module
: underline
name
: (no styling)null
: bold
number
: yellow
regexp
: red
special
: cyan
(e.g., Proxies
)string
: green
symbol
: green
undefined
: grey
Color styling uses ANSI control codes that may not be supported on all\nterminals. To verify color support use tty.hasColors()
.
Predefined control codes are listed below (grouped as \"Modifiers\", \"Foreground\ncolors\", and \"Background colors\").
", "miscs": [ { "textRaw": "Modifiers", "name": "modifiers", "desc": "Modifier support varies throughout different terminals. They will mostly be\nignored, if not supported.
\nreset
- Resets all (color) modifiers to their defaultsstrikeThrough
, crossedout
, crossedOut
)hidden
- Prints the text, but makes it invisible (Alias: conceal)faint
)swapcolors
, swapColors
)doubleUnderline
)black
red
green
yellow
blue
magenta
cyan
white
gray
(alias: grey
, blackBright
)redBright
greenBright
yellowBright
blueBright
magentaBright
cyanBright
whiteBright
bgBlack
bgRed
bgGreen
bgYellow
bgBlue
bgMagenta
bgCyan
bgWhite
bgGray
(alias: bgGrey
, bgBlackBright
)bgRedBright
bgGreenBright
bgYellowBright
bgBlueBright
bgMagentaBright
bgCyanBright
bgWhiteBright
Objects may also define their own\n[util.inspect.custom](depth, opts)
function,\nwhich util.inspect()
will invoke and use the result of when inspecting\nthe object:
const util = require('util');\n\nclass Box {\n constructor(value) {\n this.value = value;\n }\n\n [util.inspect.custom](depth, options) {\n if (depth < 0) {\n return options.stylize('[Box]', 'special');\n }\n\n const newOptions = Object.assign({}, options, {\n depth: options.depth === null ? null : options.depth - 1\n });\n\n // Five space padding because that's the size of \"Box< \".\n const padding = ' '.repeat(5);\n const inner = util.inspect(this.value, newOptions)\n .replace(/\\n/g, `\\n${padding}`);\n return `${options.stylize('Box', 'special')}< ${inner} >`;\n }\n}\n\nconst box = new Box(true);\n\nutil.inspect(box);\n// Returns: \"Box< true >\"\n
\nCustom [util.inspect.custom](depth, opts)
functions typically return a string\nbut may return a value of any type that will be formatted accordingly by\nutil.inspect()
.
const util = require('util');\n\nconst obj = { foo: 'this will not show up in the inspect() output' };\nobj[util.inspect.custom] = (depth) => {\n return { bar: 'baz' };\n};\n\nutil.inspect(obj);\n// Returns: \"{ bar: 'baz' }\"\n
"
}
],
"properties": [
{
"textRaw": "`custom` {symbol} that can be used to declare custom inspect functions.",
"type": "symbol",
"name": "custom",
"meta": {
"added": [
"v6.6.0"
],
"changes": [
{
"version": "v10.12.0",
"pr-url": "https://github.com/nodejs/node/pull/20857",
"description": "This is now defined as a shared symbol."
}
]
},
"desc": "In addition to being accessible through util.inspect.custom
, this\nsymbol is registered globally and can be\naccessed in any environment as Symbol.for('nodejs.util.inspect.custom')
.
const inspect = Symbol.for('nodejs.util.inspect.custom');\n\nclass Password {\n constructor(value) {\n this.value = value;\n }\n\n toString() {\n return 'xxxxxxxx';\n }\n\n [inspect]() {\n return `Password <${this.toString()}>`;\n }\n}\n\nconst password = new Password('r0sebud');\nconsole.log(password);\n// Prints Password <xxxxxxxx>\n
\nSee Custom inspection functions on Objects for more details.
", "shortDesc": "that can be used to declare custom inspect functions." }, { "textRaw": "`util.inspect.defaultOptions`", "name": "defaultOptions", "meta": { "added": [ "v6.4.0" ], "changes": [] }, "desc": "The defaultOptions
value allows customization of the default options used by\nutil.inspect
. This is useful for functions like console.log
or\nutil.format
which implicitly call into util.inspect
. It shall be set to an\nobject containing one or more valid util.inspect()
options. Setting\noption properties directly is also supported.
const util = require('util');\nconst arr = Array(101).fill(0);\n\nconsole.log(arr); // Logs the truncated array\nutil.inspect.defaultOptions.maxArrayLength = null;\nconsole.log(arr); // logs the full array\n
"
}
]
},
{
"textRaw": "`util.isDeepStrictEqual(val1, val2)`",
"type": "method",
"name": "isDeepStrictEqual",
"meta": {
"added": [
"v9.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`val1` {any}",
"name": "val1",
"type": "any"
},
{
"textRaw": "`val2` {any}",
"name": "val2",
"type": "any"
}
]
}
],
"desc": "Returns true
if there is deep strict equality between val1
and val2
.\nOtherwise, returns false
.
See assert.deepStrictEqual()
for more information about deep strict\nequality.
Takes a function following the common error-first callback style, i.e. taking\nan (err, value) => ...
callback as the last argument, and returns a version\nthat returns promises.
const util = require('util');\nconst fs = require('fs');\n\nconst stat = util.promisify(fs.stat);\nstat('.').then((stats) => {\n // Do something with `stats`\n}).catch((error) => {\n // Handle the error.\n});\n
\nOr, equivalently using async function
s:
const util = require('util');\nconst fs = require('fs');\n\nconst stat = util.promisify(fs.stat);\n\nasync function callStat() {\n const stats = await stat('.');\n console.log(`This directory is owned by ${stats.uid}`);\n}\n
\nIf there is an original[util.promisify.custom]
property present, promisify
\nwill return its value, see Custom promisified functions.
promisify()
assumes that original
is a function taking a callback as its\nfinal argument in all cases. If original
is not a function, promisify()
\nwill throw an error. If original
is a function but its last argument is not\nan error-first callback, it will still be passed an error-first\ncallback as its last argument.
Using promisify()
on class methods or other methods that use this
may not\nwork as expected unless handled specially:
const util = require('util');\n\nclass Foo {\n constructor() {\n this.a = 42;\n }\n\n bar(callback) {\n callback(null, this.a);\n }\n}\n\nconst foo = new Foo();\n\nconst naiveBar = util.promisify(foo.bar);\n// TypeError: Cannot read property 'a' of undefined\n// naiveBar().then(a => console.log(a));\n\nnaiveBar.call(foo).then((a) => console.log(a)); // '42'\n\nconst bindBar = naiveBar.bind(foo);\nbindBar().then((a) => console.log(a)); // '42'\n
",
"modules": [
{
"textRaw": "Custom promisified functions",
"name": "custom_promisified_functions",
"desc": "Using the util.promisify.custom
symbol one can override the return value of\nutil.promisify()
:
const util = require('util');\n\nfunction doSomething(foo, callback) {\n // ...\n}\n\ndoSomething[util.promisify.custom] = (foo) => {\n return getPromiseSomehow();\n};\n\nconst promisified = util.promisify(doSomething);\nconsole.log(promisified === doSomething[util.promisify.custom]);\n// prints 'true'\n
\nThis can be useful for cases where the original function does not follow the\nstandard format of taking an error-first callback as the last argument.
\nFor example, with a function that takes in\n(foo, onSuccessCallback, onErrorCallback)
:
doSomething[util.promisify.custom] = (foo) => {\n return new Promise((resolve, reject) => {\n doSomething(foo, resolve, reject);\n });\n};\n
\nIf promisify.custom
is defined but is not a function, promisify()
will\nthrow an error.
In addition to being accessible through util.promisify.custom
, this\nsymbol is registered globally and can be\naccessed in any environment as Symbol.for('nodejs.util.promisify.custom')
.
For example, with a function that takes in\n(foo, onSuccessCallback, onErrorCallback)
:
const kCustomPromisifiedSymbol = Symbol.for('nodejs.util.promisify.custom');\n\ndoSomething[kCustomPromisifiedSymbol] = (foo) => {\n return new Promise((resolve, reject) => {\n doSomething(foo, resolve, reject);\n });\n};\n
",
"shortDesc": "that can be used to declare custom promisified variants of functions, see [Custom promisified functions][]."
}
]
}
],
"classes": [
{
"textRaw": "Class: `util.TextDecoder`",
"type": "class",
"name": "util.TextDecoder",
"meta": {
"added": [
"v8.3.0"
],
"changes": []
},
"desc": "An implementation of the WHATWG Encoding Standard TextDecoder
API.
const decoder = new TextDecoder('shift_jis');\nlet string = '';\nlet buffer;\nwhile (buffer = getNextChunkSomehow()) {\n string += decoder.decode(buffer, { stream: true });\n}\nstring += decoder.decode(); // end-of-stream\n
",
"modules": [
{
"textRaw": "WHATWG supported encodings",
"name": "whatwg_supported_encodings",
"desc": "Per the WHATWG Encoding Standard, the encodings supported by the\nTextDecoder
API are outlined in the tables below. For each encoding,\none or more aliases may be used.
Different Node.js build configurations support different sets of encodings.\n(see Internationalization)
", "modules": [ { "textRaw": "Encodings supported by default (with full ICU data)", "name": "encodings_supported_by_default_(with_full_icu_data)", "desc": "Encoding | \nAliases | \n
---|---|
'ibm866' | \n'866' , 'cp866' , 'csibm866' | \n
'iso-8859-2' | \n'csisolatin2' , 'iso-ir-101' , 'iso8859-2' , 'iso88592' , 'iso_8859-2' , 'iso_8859-2:1987' , 'l2' , 'latin2' | \n
'iso-8859-3' | \n'csisolatin3' , 'iso-ir-109' , 'iso8859-3' , 'iso88593' , 'iso_8859-3' , 'iso_8859-3:1988' , 'l3' , 'latin3' | \n
'iso-8859-4' | \n'csisolatin4' , 'iso-ir-110' , 'iso8859-4' , 'iso88594' , 'iso_8859-4' , 'iso_8859-4:1988' , 'l4' , 'latin4' | \n
'iso-8859-5' | \n'csisolatincyrillic' , 'cyrillic' , 'iso-ir-144' , 'iso8859-5' , 'iso88595' , 'iso_8859-5' , 'iso_8859-5:1988' | \n
'iso-8859-6' | \n'arabic' , 'asmo-708' , 'csiso88596e' , 'csiso88596i' , 'csisolatinarabic' , 'ecma-114' , 'iso-8859-6-e' , 'iso-8859-6-i' , 'iso-ir-127' , 'iso8859-6' , 'iso88596' , 'iso_8859-6' , 'iso_8859-6:1987' | \n
'iso-8859-7' | \n'csisolatingreek' , 'ecma-118' , 'elot_928' , 'greek' , 'greek8' , 'iso-ir-126' , 'iso8859-7' , 'iso88597' , 'iso_8859-7' , 'iso_8859-7:1987' , 'sun_eu_greek' | \n
'iso-8859-8' | \n'csiso88598e' , 'csisolatinhebrew' , 'hebrew' , 'iso-8859-8-e' , 'iso-ir-138' , 'iso8859-8' , 'iso88598' , 'iso_8859-8' , 'iso_8859-8:1988' , 'visual' | \n
'iso-8859-8-i' | \n'csiso88598i' , 'logical' | \n
'iso-8859-10' | \n'csisolatin6' , 'iso-ir-157' , 'iso8859-10' , 'iso885910' , 'l6' , 'latin6' | \n
'iso-8859-13' | \n'iso8859-13' , 'iso885913' | \n
'iso-8859-14' | \n'iso8859-14' , 'iso885914' | \n
'iso-8859-15' | \n'csisolatin9' , 'iso8859-15' , 'iso885915' , 'iso_8859-15' , 'l9' | \n
'koi8-r' | \n'cskoi8r' , 'koi' , 'koi8' , 'koi8_r' | \n
'koi8-u' | \n'koi8-ru' | \n
'macintosh' | \n'csmacintosh' , 'mac' , 'x-mac-roman' | \n
'windows-874' | \n'dos-874' , 'iso-8859-11' , 'iso8859-11' , 'iso885911' , 'tis-620' | \n
'windows-1250' | \n'cp1250' , 'x-cp1250' | \n
'windows-1251' | \n'cp1251' , 'x-cp1251' | \n
'windows-1252' | \n'ansi_x3.4-1968' , 'ascii' , 'cp1252' , 'cp819' , 'csisolatin1' , 'ibm819' , 'iso-8859-1' , 'iso-ir-100' , 'iso8859-1' , 'iso88591' , 'iso_8859-1' , 'iso_8859-1:1987' , 'l1' , 'latin1' , 'us-ascii' , 'x-cp1252' | \n
'windows-1253' | \n'cp1253' , 'x-cp1253' | \n
'windows-1254' | \n'cp1254' , 'csisolatin5' , 'iso-8859-9' , 'iso-ir-148' , 'iso8859-9' , 'iso88599' , 'iso_8859-9' , 'iso_8859-9:1989' , 'l5' , 'latin5' , 'x-cp1254' | \n
'windows-1255' | \n'cp1255' , 'x-cp1255' | \n
'windows-1256' | \n'cp1256' , 'x-cp1256' | \n
'windows-1257' | \n'cp1257' , 'x-cp1257' | \n
'windows-1258' | \n'cp1258' , 'x-cp1258' | \n
'x-mac-cyrillic' | \n'x-mac-ukrainian' | \n
'gbk' | \n'chinese' , 'csgb2312' , 'csiso58gb231280' , 'gb2312' , 'gb_2312' , 'gb_2312-80' , 'iso-ir-58' , 'x-gbk' | \n
'gb18030' | \n\n |
'big5' | \n'big5-hkscs' , 'cn-big5' , 'csbig5' , 'x-x-big5' | \n
'euc-jp' | \n'cseucpkdfmtjapanese' , 'x-euc-jp' | \n
'iso-2022-jp' | \n'csiso2022jp' | \n
'shift_jis' | \n'csshiftjis' , 'ms932' , 'ms_kanji' , 'shift-jis' , 'sjis' , 'windows-31j' , 'x-sjis' | \n
'euc-kr' | \n'cseuckr' , 'csksc56011987' , 'iso-ir-149' , 'korean' , 'ks_c_5601-1987' , 'ks_c_5601-1989' , 'ksc5601' , 'ksc_5601' , 'windows-949' | \n
Encoding | \nAliases | \n
---|---|
'utf-8' | \n'unicode-1-1-utf-8' , 'utf8' | \n
'utf-16le' | \n'utf-16' | \n
'utf-16be' | \n\n |
Encoding | \nAliases | \n
---|---|
'utf-8' | \n'unicode-1-1-utf-8' , 'utf8' | \n
'utf-16le' | \n'utf-16' | \n
The 'iso-8859-16'
encoding listed in the WHATWG Encoding Standard\nis not supported.
Decodes the input
and returns a string. If options.stream
is true
, any\nincomplete byte sequences occurring at the end of the input
are buffered\ninternally and emitted after the next call to textDecoder.decode()
.
If textDecoder.fatal
is true
, decoding errors that occur will result in a\nTypeError
being thrown.
The encoding supported by the TextDecoder
instance.
The value will be true
if decoding errors result in a TypeError
being\nthrown.
The value will be true
if the decoding result will include the byte order\nmark.
Creates an new TextDecoder
instance. The encoding
may specify one of the\nsupported encodings or an alias.
The TextDecoder
class is also available on the global object.
An implementation of the WHATWG Encoding Standard TextEncoder
API. All\ninstances of TextEncoder
only support UTF-8 encoding.
const encoder = new TextEncoder();\nconst uint8array = encoder.encode('this is some data');\n
\nThe TextEncoder
class is also available on the global object.
UTF-8 encodes the input
string and returns a Uint8Array
containing the\nencoded bytes.
UTF-8 encodes the src
string to the dest
Uint8Array and returns an object\ncontaining the read Unicode code units and written UTF-8 bytes.
const encoder = new TextEncoder();\nconst src = 'this is some data';\nconst dest = new Uint8Array(10);\nconst { read, written } = encoder.encodeInto(src, dest);\n
"
}
],
"properties": [
{
"textRaw": "`encoding` {string}",
"type": "string",
"name": "encoding",
"desc": "The encoding supported by the TextEncoder
instance. Always set to 'utf-8'
.
util.types
provides type checks for different kinds of built-in objects.\nUnlike instanceof
or Object.prototype.toString.call(value)
, these checks do\nnot inspect properties of the object that are accessible from JavaScript (like\ntheir prototype), and usually have the overhead of calling into C++.
The result generally does not make any guarantees about what kinds of\nproperties or behavior a value exposes in JavaScript. They are primarily\nuseful for addon developers who prefer to do type checking in JavaScript.
\nThe API is accessible via require('util').types
or require('util/types')
.
Returns true
if the value is a built-in ArrayBuffer
or\nSharedArrayBuffer
instance.
See also util.types.isArrayBuffer()
and\nutil.types.isSharedArrayBuffer()
.
util.types.isAnyArrayBuffer(new ArrayBuffer()); // Returns true\nutil.types.isAnyArrayBuffer(new SharedArrayBuffer()); // Returns true\n
"
},
{
"textRaw": "`util.types.isArrayBufferView(value)`",
"type": "method",
"name": "isArrayBufferView",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is an instance of one of the ArrayBuffer
\nviews, such as typed array objects or DataView
. Equivalent to\nArrayBuffer.isView()
.
util.types.isArrayBufferView(new Int8Array()); // true\nutil.types.isArrayBufferView(Buffer.from('hello world')); // true\nutil.types.isArrayBufferView(new DataView(new ArrayBuffer(16))); // true\nutil.types.isArrayBufferView(new ArrayBuffer()); // false\n
"
},
{
"textRaw": "`util.types.isArgumentsObject(value)`",
"type": "method",
"name": "isArgumentsObject",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is an arguments
object.
function foo() {\n util.types.isArgumentsObject(arguments); // Returns true\n}\n
"
},
{
"textRaw": "`util.types.isArrayBuffer(value)`",
"type": "method",
"name": "isArrayBuffer",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in ArrayBuffer
instance.\nThis does not include SharedArrayBuffer
instances. Usually, it is\ndesirable to test for both; See util.types.isAnyArrayBuffer()
for that.
util.types.isArrayBuffer(new ArrayBuffer()); // Returns true\nutil.types.isArrayBuffer(new SharedArrayBuffer()); // Returns false\n
"
},
{
"textRaw": "`util.types.isAsyncFunction(value)`",
"type": "method",
"name": "isAsyncFunction",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is an async function.\nThis only reports back what the JavaScript engine is seeing;\nin particular, the return value may not match the original source code if\na transpilation tool was used.
util.types.isAsyncFunction(function foo() {}); // Returns false\nutil.types.isAsyncFunction(async function foo() {}); // Returns true\n
"
},
{
"textRaw": "`util.types.isBigInt64Array(value)`",
"type": "method",
"name": "isBigInt64Array",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a BigInt64Array
instance.
util.types.isBigInt64Array(new BigInt64Array()); // Returns true\nutil.types.isBigInt64Array(new BigUint64Array()); // Returns false\n
"
},
{
"textRaw": "`util.types.isBigUint64Array(value)`",
"type": "method",
"name": "isBigUint64Array",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a BigUint64Array
instance.
util.types.isBigUint64Array(new BigInt64Array()); // Returns false\nutil.types.isBigUint64Array(new BigUint64Array()); // Returns true\n
"
},
{
"textRaw": "`util.types.isBooleanObject(value)`",
"type": "method",
"name": "isBooleanObject",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a boolean object, e.g. created\nby new Boolean()
.
util.types.isBooleanObject(false); // Returns false\nutil.types.isBooleanObject(true); // Returns false\nutil.types.isBooleanObject(new Boolean(false)); // Returns true\nutil.types.isBooleanObject(new Boolean(true)); // Returns true\nutil.types.isBooleanObject(Boolean(false)); // Returns false\nutil.types.isBooleanObject(Boolean(true)); // Returns false\n
"
},
{
"textRaw": "`util.types.isBoxedPrimitive(value)`",
"type": "method",
"name": "isBoxedPrimitive",
"meta": {
"added": [
"v10.11.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is any boxed primitive object, e.g. created\nby new Boolean()
, new String()
or Object(Symbol())
.
For example:
\nutil.types.isBoxedPrimitive(false); // Returns false\nutil.types.isBoxedPrimitive(new Boolean(false)); // Returns true\nutil.types.isBoxedPrimitive(Symbol('foo')); // Returns false\nutil.types.isBoxedPrimitive(Object(Symbol('foo'))); // Returns true\nutil.types.isBoxedPrimitive(Object(BigInt(5))); // Returns true\n
"
},
{
"textRaw": "`util.types.isDataView(value)`",
"type": "method",
"name": "isDataView",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in DataView
instance.
const ab = new ArrayBuffer(20);\nutil.types.isDataView(new DataView(ab)); // Returns true\nutil.types.isDataView(new Float64Array()); // Returns false\n
\nSee also ArrayBuffer.isView()
.
Returns true
if the value is a built-in Date
instance.
util.types.isDate(new Date()); // Returns true\n
"
},
{
"textRaw": "`util.types.isExternal(value)`",
"type": "method",
"name": "isExternal",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a native External
value.
A native External
value is a special type of object that contains a\nraw C++ pointer (void*
) for access from native code, and has no other\nproperties. Such objects are created either by Node.js internals or native\naddons. In JavaScript, they are frozen objects with a\nnull
prototype.
#include <js_native_api.h>\n#include <stdlib.h>\nnapi_value result;\nstatic napi_value MyNapi(napi_env env, napi_callback_info info) {\n int* raw = (int*) malloc(1024);\n napi_status status = napi_create_external(env, (void*) raw, NULL, NULL, &result);\n if (status != napi_ok) {\n napi_throw_error(env, NULL, \"napi_create_external failed\");\n return NULL;\n }\n return result;\n}\n...\nDECLARE_NAPI_PROPERTY(\"myNapi\", MyNapi)\n...\n
\nconst native = require('napi_addon.node');\nconst data = native.myNapi();\nutil.types.isExternal(data); // returns true\nutil.types.isExternal(0); // returns false\nutil.types.isExternal(new String('foo')); // returns false\n
\nFor further information on napi_create_external
, refer to\nnapi_create_external()
.
Returns true
if the value is a built-in Float32Array
instance.
util.types.isFloat32Array(new ArrayBuffer()); // Returns false\nutil.types.isFloat32Array(new Float32Array()); // Returns true\nutil.types.isFloat32Array(new Float64Array()); // Returns false\n
"
},
{
"textRaw": "`util.types.isFloat64Array(value)`",
"type": "method",
"name": "isFloat64Array",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Float64Array
instance.
util.types.isFloat64Array(new ArrayBuffer()); // Returns false\nutil.types.isFloat64Array(new Uint8Array()); // Returns false\nutil.types.isFloat64Array(new Float64Array()); // Returns true\n
"
},
{
"textRaw": "`util.types.isGeneratorFunction(value)`",
"type": "method",
"name": "isGeneratorFunction",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a generator function.\nThis only reports back what the JavaScript engine is seeing;\nin particular, the return value may not match the original source code if\na transpilation tool was used.
util.types.isGeneratorFunction(function foo() {}); // Returns false\nutil.types.isGeneratorFunction(function* foo() {}); // Returns true\n
"
},
{
"textRaw": "`util.types.isGeneratorObject(value)`",
"type": "method",
"name": "isGeneratorObject",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a generator object as returned from a\nbuilt-in generator function.\nThis only reports back what the JavaScript engine is seeing;\nin particular, the return value may not match the original source code if\na transpilation tool was used.
function* foo() {}\nconst generator = foo();\nutil.types.isGeneratorObject(generator); // Returns true\n
"
},
{
"textRaw": "`util.types.isInt8Array(value)`",
"type": "method",
"name": "isInt8Array",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Int8Array
instance.
util.types.isInt8Array(new ArrayBuffer()); // Returns false\nutil.types.isInt8Array(new Int8Array()); // Returns true\nutil.types.isInt8Array(new Float64Array()); // Returns false\n
"
},
{
"textRaw": "`util.types.isInt16Array(value)`",
"type": "method",
"name": "isInt16Array",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Int16Array
instance.
util.types.isInt16Array(new ArrayBuffer()); // Returns false\nutil.types.isInt16Array(new Int16Array()); // Returns true\nutil.types.isInt16Array(new Float64Array()); // Returns false\n
"
},
{
"textRaw": "`util.types.isInt32Array(value)`",
"type": "method",
"name": "isInt32Array",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Int32Array
instance.
util.types.isInt32Array(new ArrayBuffer()); // Returns false\nutil.types.isInt32Array(new Int32Array()); // Returns true\nutil.types.isInt32Array(new Float64Array()); // Returns false\n
"
},
{
"textRaw": "`util.types.isMap(value)`",
"type": "method",
"name": "isMap",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Map
instance.
util.types.isMap(new Map()); // Returns true\n
"
},
{
"textRaw": "`util.types.isMapIterator(value)`",
"type": "method",
"name": "isMapIterator",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is an iterator returned for a built-in\nMap
instance.
const map = new Map();\nutil.types.isMapIterator(map.keys()); // Returns true\nutil.types.isMapIterator(map.values()); // Returns true\nutil.types.isMapIterator(map.entries()); // Returns true\nutil.types.isMapIterator(map[Symbol.iterator]()); // Returns true\n
"
},
{
"textRaw": "`util.types.isModuleNamespaceObject(value)`",
"type": "method",
"name": "isModuleNamespaceObject",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is an instance of a Module Namespace Object.
import * as ns from './a.js';\n\nutil.types.isModuleNamespaceObject(ns); // Returns true\n
"
},
{
"textRaw": "`util.types.isNativeError(value)`",
"type": "method",
"name": "isNativeError",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is an instance of a built-in Error
type.
util.types.isNativeError(new Error()); // Returns true\nutil.types.isNativeError(new TypeError()); // Returns true\nutil.types.isNativeError(new RangeError()); // Returns true\n
"
},
{
"textRaw": "`util.types.isNumberObject(value)`",
"type": "method",
"name": "isNumberObject",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a number object, e.g. created\nby new Number()
.
util.types.isNumberObject(0); // Returns false\nutil.types.isNumberObject(new Number(0)); // Returns true\n
"
},
{
"textRaw": "`util.types.isPromise(value)`",
"type": "method",
"name": "isPromise",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Promise
.
util.types.isPromise(Promise.resolve(42)); // Returns true\n
"
},
{
"textRaw": "`util.types.isProxy(value)`",
"type": "method",
"name": "isProxy",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a Proxy
instance.
const target = {};\nconst proxy = new Proxy(target, {});\nutil.types.isProxy(target); // Returns false\nutil.types.isProxy(proxy); // Returns true\n
"
},
{
"textRaw": "`util.types.isRegExp(value)`",
"type": "method",
"name": "isRegExp",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a regular expression object.
util.types.isRegExp(/abc/); // Returns true\nutil.types.isRegExp(new RegExp('abc')); // Returns true\n
"
},
{
"textRaw": "`util.types.isSet(value)`",
"type": "method",
"name": "isSet",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Set
instance.
util.types.isSet(new Set()); // Returns true\n
"
},
{
"textRaw": "`util.types.isSetIterator(value)`",
"type": "method",
"name": "isSetIterator",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is an iterator returned for a built-in\nSet
instance.
const set = new Set();\nutil.types.isSetIterator(set.keys()); // Returns true\nutil.types.isSetIterator(set.values()); // Returns true\nutil.types.isSetIterator(set.entries()); // Returns true\nutil.types.isSetIterator(set[Symbol.iterator]()); // Returns true\n
"
},
{
"textRaw": "`util.types.isSharedArrayBuffer(value)`",
"type": "method",
"name": "isSharedArrayBuffer",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in SharedArrayBuffer
instance.\nThis does not include ArrayBuffer
instances. Usually, it is\ndesirable to test for both; See util.types.isAnyArrayBuffer()
for that.
util.types.isSharedArrayBuffer(new ArrayBuffer()); // Returns false\nutil.types.isSharedArrayBuffer(new SharedArrayBuffer()); // Returns true\n
"
},
{
"textRaw": "`util.types.isStringObject(value)`",
"type": "method",
"name": "isStringObject",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a string object, e.g. created\nby new String()
.
util.types.isStringObject('foo'); // Returns false\nutil.types.isStringObject(new String('foo')); // Returns true\n
"
},
{
"textRaw": "`util.types.isSymbolObject(value)`",
"type": "method",
"name": "isSymbolObject",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a symbol object, created\nby calling Object()
on a Symbol
primitive.
const symbol = Symbol('foo');\nutil.types.isSymbolObject(symbol); // Returns false\nutil.types.isSymbolObject(Object(symbol)); // Returns true\n
"
},
{
"textRaw": "`util.types.isTypedArray(value)`",
"type": "method",
"name": "isTypedArray",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in TypedArray
instance.
util.types.isTypedArray(new ArrayBuffer()); // Returns false\nutil.types.isTypedArray(new Uint8Array()); // Returns true\nutil.types.isTypedArray(new Float64Array()); // Returns true\n
\nSee also ArrayBuffer.isView()
.
Returns true
if the value is a built-in Uint8Array
instance.
util.types.isUint8Array(new ArrayBuffer()); // Returns false\nutil.types.isUint8Array(new Uint8Array()); // Returns true\nutil.types.isUint8Array(new Float64Array()); // Returns false\n
"
},
{
"textRaw": "`util.types.isUint8ClampedArray(value)`",
"type": "method",
"name": "isUint8ClampedArray",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Uint8ClampedArray
instance.
util.types.isUint8ClampedArray(new ArrayBuffer()); // Returns false\nutil.types.isUint8ClampedArray(new Uint8ClampedArray()); // Returns true\nutil.types.isUint8ClampedArray(new Float64Array()); // Returns false\n
"
},
{
"textRaw": "`util.types.isUint16Array(value)`",
"type": "method",
"name": "isUint16Array",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Uint16Array
instance.
util.types.isUint16Array(new ArrayBuffer()); // Returns false\nutil.types.isUint16Array(new Uint16Array()); // Returns true\nutil.types.isUint16Array(new Float64Array()); // Returns false\n
"
},
{
"textRaw": "`util.types.isUint32Array(value)`",
"type": "method",
"name": "isUint32Array",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in Uint32Array
instance.
util.types.isUint32Array(new ArrayBuffer()); // Returns false\nutil.types.isUint32Array(new Uint32Array()); // Returns true\nutil.types.isUint32Array(new Float64Array()); // Returns false\n
"
},
{
"textRaw": "`util.types.isWeakMap(value)`",
"type": "method",
"name": "isWeakMap",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in WeakMap
instance.
util.types.isWeakMap(new WeakMap()); // Returns true\n
"
},
{
"textRaw": "`util.types.isWeakSet(value)`",
"type": "method",
"name": "isWeakSet",
"meta": {
"added": [
"v10.0.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in WeakSet
instance.
util.types.isWeakSet(new WeakSet()); // Returns true\n
"
},
{
"textRaw": "`util.types.isWebAssemblyCompiledModule(value)`",
"type": "method",
"name": "isWebAssemblyCompiledModule",
"meta": {
"added": [
"v10.0.0"
],
"deprecated": [
"v14.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use `value instanceof WebAssembly.Module` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`value` {any}",
"name": "value",
"type": "any"
}
]
}
],
"desc": "Returns true
if the value is a built-in WebAssembly.Module
instance.
const module = new WebAssembly.Module(wasmBuffer);\nutil.types.isWebAssemblyCompiledModule(module); // Returns true\n
"
}
]
}
],
"modules": [
{
"textRaw": "Deprecated APIs",
"name": "deprecated_apis",
"desc": "The following APIs are deprecated and should no longer be used. Existing\napplications and modules should be updated to find alternative approaches.
", "methods": [ { "textRaw": "`util._extend(target, source)`", "type": "method", "name": "_extend", "meta": { "added": [ "v0.7.5" ], "deprecated": [ "v6.0.0" ], "changes": [] }, "stability": 0, "stabilityText": "Deprecated: Use [`Object.assign()`][] instead.", "signatures": [ { "params": [ { "textRaw": "`target` {Object}", "name": "target", "type": "Object" }, { "textRaw": "`source` {Object}", "name": "source", "type": "Object" } ] } ], "desc": "The util._extend()
method was never intended to be used outside of internal\nNode.js modules. The community found and used it anyway.
It is deprecated and should not be used in new code. JavaScript comes with very\nsimilar built-in functionality through Object.assign()
.
Alias for Array.isArray()
.
Returns true
if the given object
is an Array
. Otherwise, returns false
.
const util = require('util');\n\nutil.isArray([]);\n// Returns: true\nutil.isArray(new Array());\n// Returns: true\nutil.isArray({});\n// Returns: false\n
"
},
{
"textRaw": "`util.isBoolean(object)`",
"type": "method",
"name": "isBoolean",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use `typeof value === 'boolean'` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is a Boolean
. Otherwise, returns false
.
const util = require('util');\n\nutil.isBoolean(1);\n// Returns: false\nutil.isBoolean(0);\n// Returns: false\nutil.isBoolean(false);\n// Returns: true\n
"
},
{
"textRaw": "`util.isBuffer(object)`",
"type": "method",
"name": "isBuffer",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use [`Buffer.isBuffer()`][] instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is a Buffer
. Otherwise, returns false
.
const util = require('util');\n\nutil.isBuffer({ length: 0 });\n// Returns: false\nutil.isBuffer([]);\n// Returns: false\nutil.isBuffer(Buffer.from('hello world'));\n// Returns: true\n
"
},
{
"textRaw": "`util.isDate(object)`",
"type": "method",
"name": "isDate",
"meta": {
"added": [
"v0.6.0"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use [`util.types.isDate()`][] instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is a Date
. Otherwise, returns false
.
const util = require('util');\n\nutil.isDate(new Date());\n// Returns: true\nutil.isDate(Date());\n// false (without 'new' returns a String)\nutil.isDate({});\n// Returns: false\n
"
},
{
"textRaw": "`util.isError(object)`",
"type": "method",
"name": "isError",
"meta": {
"added": [
"v0.6.0"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use [`util.types.isNativeError()`][] instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is an Error
. Otherwise, returns\nfalse
.
const util = require('util');\n\nutil.isError(new Error());\n// Returns: true\nutil.isError(new TypeError());\n// Returns: true\nutil.isError({ name: 'Error', message: 'an error occurred' });\n// Returns: false\n
\nThis method relies on Object.prototype.toString()
behavior. It is\npossible to obtain an incorrect result when the object
argument manipulates\n@@toStringTag
.
const util = require('util');\nconst obj = { name: 'Error', message: 'an error occurred' };\n\nutil.isError(obj);\n// Returns: false\nobj[Symbol.toStringTag] = 'Error';\nutil.isError(obj);\n// Returns: true\n
"
},
{
"textRaw": "`util.isFunction(object)`",
"type": "method",
"name": "isFunction",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use `typeof value === 'function'` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is a Function
. Otherwise, returns\nfalse
.
const util = require('util');\n\nfunction Foo() {}\nconst Bar = () => {};\n\nutil.isFunction({});\n// Returns: false\nutil.isFunction(Foo);\n// Returns: true\nutil.isFunction(Bar);\n// Returns: true\n
"
},
{
"textRaw": "`util.isNull(object)`",
"type": "method",
"name": "isNull",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use `value === null` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is strictly null
. Otherwise, returns\nfalse
.
const util = require('util');\n\nutil.isNull(0);\n// Returns: false\nutil.isNull(undefined);\n// Returns: false\nutil.isNull(null);\n// Returns: true\n
"
},
{
"textRaw": "`util.isNullOrUndefined(object)`",
"type": "method",
"name": "isNullOrUndefined",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use\n`value === undefined || value === null` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is null
or undefined
. Otherwise,\nreturns false
.
const util = require('util');\n\nutil.isNullOrUndefined(0);\n// Returns: false\nutil.isNullOrUndefined(undefined);\n// Returns: true\nutil.isNullOrUndefined(null);\n// Returns: true\n
"
},
{
"textRaw": "`util.isNumber(object)`",
"type": "method",
"name": "isNumber",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use `typeof value === 'number'` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is a Number
. Otherwise, returns false
.
const util = require('util');\n\nutil.isNumber(false);\n// Returns: false\nutil.isNumber(Infinity);\n// Returns: true\nutil.isNumber(0);\n// Returns: true\nutil.isNumber(NaN);\n// Returns: true\n
"
},
{
"textRaw": "`util.isObject(object)`",
"type": "method",
"name": "isObject",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated:\nUse `value !== null && typeof value === 'object'` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is strictly an Object
and not a\nFunction
(even though functions are objects in JavaScript).\nOtherwise, returns false
.
const util = require('util');\n\nutil.isObject(5);\n// Returns: false\nutil.isObject(null);\n// Returns: false\nutil.isObject({});\n// Returns: true\nutil.isObject(() => {});\n// Returns: false\n
"
},
{
"textRaw": "`util.isPrimitive(object)`",
"type": "method",
"name": "isPrimitive",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use\n`(typeof value !== 'object' && typeof value !== 'function') || value === null`\ninstead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is a primitive type. Otherwise, returns\nfalse
.
const util = require('util');\n\nutil.isPrimitive(5);\n// Returns: true\nutil.isPrimitive('foo');\n// Returns: true\nutil.isPrimitive(false);\n// Returns: true\nutil.isPrimitive(null);\n// Returns: true\nutil.isPrimitive(undefined);\n// Returns: true\nutil.isPrimitive({});\n// Returns: false\nutil.isPrimitive(() => {});\n// Returns: false\nutil.isPrimitive(/^$/);\n// Returns: false\nutil.isPrimitive(new Date());\n// Returns: false\n
"
},
{
"textRaw": "`util.isRegExp(object)`",
"type": "method",
"name": "isRegExp",
"meta": {
"added": [
"v0.6.0"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is a RegExp
. Otherwise, returns false
.
const util = require('util');\n\nutil.isRegExp(/some regexp/);\n// Returns: true\nutil.isRegExp(new RegExp('another regexp'));\n// Returns: true\nutil.isRegExp({});\n// Returns: false\n
"
},
{
"textRaw": "`util.isString(object)`",
"type": "method",
"name": "isString",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use `typeof value === 'string'` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is a string
. Otherwise, returns false
.
const util = require('util');\n\nutil.isString('');\n// Returns: true\nutil.isString('foo');\n// Returns: true\nutil.isString(String('foo'));\n// Returns: true\nutil.isString(5);\n// Returns: false\n
"
},
{
"textRaw": "`util.isSymbol(object)`",
"type": "method",
"name": "isSymbol",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use `typeof value === 'symbol'` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is a Symbol
. Otherwise, returns false
.
const util = require('util');\n\nutil.isSymbol(5);\n// Returns: false\nutil.isSymbol('foo');\n// Returns: false\nutil.isSymbol(Symbol('foo'));\n// Returns: true\n
"
},
{
"textRaw": "`util.isUndefined(object)`",
"type": "method",
"name": "isUndefined",
"meta": {
"added": [
"v0.11.5"
],
"deprecated": [
"v4.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use `value === undefined` instead.",
"signatures": [
{
"return": {
"textRaw": "Returns: {boolean}",
"name": "return",
"type": "boolean"
},
"params": [
{
"textRaw": "`object` {any}",
"name": "object",
"type": "any"
}
]
}
],
"desc": "Returns true
if the given object
is undefined
. Otherwise, returns false
.
const util = require('util');\n\nconst foo = undefined;\nutil.isUndefined(5);\n// Returns: false\nutil.isUndefined(foo);\n// Returns: true\nutil.isUndefined(null);\n// Returns: false\n
"
},
{
"textRaw": "`util.log(string)`",
"type": "method",
"name": "log",
"meta": {
"added": [
"v0.3.0"
],
"deprecated": [
"v6.0.0"
],
"changes": []
},
"stability": 0,
"stabilityText": "Deprecated: Use a third party module instead.",
"signatures": [
{
"params": [
{
"textRaw": "`string` {string}",
"name": "string",
"type": "string"
}
]
}
],
"desc": "The util.log()
method prints the given string
to stdout
with an included\ntimestamp.
const util = require('util');\n\nutil.log('Timestamped message.');\n
"
}
],
"type": "module",
"displayName": "Deprecated APIs"
}
],
"type": "module",
"displayName": "Util"
}
]
}