{ "source": "doc/api/util.md", "modules": [ { "textRaw": "util", "name": "util", "stability": 2, "stabilityText": "Stable", "desc": "
These functions are in the module 'util'
. Use require('util')
to\naccess them.\n\n
The util
module is primarily designed to support the needs of Node.js's\ninternal APIs. Many of these utilities are useful for your own\nprograms. If you find that these functions are lacking for your\npurposes, however, you are encouraged to write your own utilities. We\nare not interested in any future additions to the util
module that\nare unnecessary for Node.js's internal functionality.\n\n
Deprecated predecessor of console.error
.\n\n
This is used to create a function which conditionally writes to stderr\nbased on the existence of a NODE_DEBUG
environment variable. If the\nsection
name appears in that environment variable, then the returned\nfunction will be similar to console.error()
. If not, then the\nreturned function is a no-op.\n\n
For example:\n\n
\nvar debuglog = util.debuglog('foo');\n\nvar bar = 123;\ndebuglog('hello from foo [%d]', bar);
\nIf this program is run with NODE_DEBUG=foo
in the environment, then\nit will output something like:\n\n
FOO 3245: hello from foo [123]
\nwhere 3245
is the process id. If it is not run with that\nenvironment variable set, then it will not print anything.\n\n
You may separate multiple NODE_DEBUG
environment variables with a\ncomma. For example, NODE_DEBUG=fs,net,tls
.\n\n
Marks that a method should not be used any more.\n\n
\nconst util = require('util');\n\nexports.puts = util.deprecate(() => {\n for (var i = 0, len = arguments.length; i < len; ++i) {\n process.stdout.write(arguments[i] + '\\n');\n }\n}, 'util.puts: Use console.log instead');
\nIt returns a modified function which warns once by default.\n\n
\nIf --no-deprecation
is set then this function is a NO-OP. Configurable\nat run-time through the process.noDeprecation
boolean (only effective\nwhen set before a module is loaded.)\n\n
If --trace-deprecation
is set, a warning and a stack trace are logged\nto the console the first time the deprecated API is used. Configurable\nat run-time through the process.traceDeprecation
boolean.\n\n
If --throw-deprecation
is set then the application throws an exception\nwhen the deprecated API is used. Configurable at run-time through the\nprocess.throwDeprecation
boolean.\n\n
process.throwDeprecation
takes precedence over process.traceDeprecation
.\n\n
Deprecated predecessor of console.error
.\n\n
Returns a formatted string using the first argument as a printf
-like format.\n\n
The first argument is a string that contains zero or more placeholders.\nEach placeholder is replaced with the converted value from its corresponding\nargument. Supported placeholders are:\n\n
\n%s
- String.%d
- Number (both integer and float).%j
- JSON. Replaced with the string '[Circular]'
if the argument\ncontains circular references.%%
- single percent sign ('%'
). This does not consume an argument.If the placeholder does not have a corresponding argument, the placeholder is\nnot replaced.\n\n
\nutil.format('%s:%s', 'foo'); // 'foo:%s'
\nIf there are more arguments than placeholders, the extra arguments are\ncoerced to strings (for objects and symbols, util.inspect()
is used)\nand then concatenated, delimited by a space.\n\n
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
\nIf the first argument is not a format string then util.format()
returns\na string that is the concatenation of all its arguments separated by spaces.\nEach argument is converted to a string with util.inspect()
.\n\n
util.format(1, 2, 3); // '1 2 3'
\n",
"signatures": [
{
"params": [
{
"name": "format"
},
{
"name": "...",
"optional": true
}
]
}
]
},
{
"textRaw": "util.inherits(constructor, superConstructor)",
"type": "method",
"name": "inherits",
"desc": "Inherit the prototype methods from one [constructor][] into another. The\nprototype of constructor
will be set to a new object created from\nsuperConstructor
.\n\n
As an additional convenience, superConstructor
will be accessible\nthrough the constructor.super_
property.\n\n
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\nvar 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",
"signatures": [
{
"params": [
{
"name": "constructor"
},
{
"name": "superConstructor"
}
]
}
]
},
{
"textRaw": "util.inspect(object[, options])",
"type": "method",
"name": "inspect",
"desc": "Return a string representation of object
, which is useful for debugging.\n\n
An optional options object may be passed that alters certain aspects of the\nformatted string:\n\n
\nshowHidden
- if true
then the object's non-enumerable and symbol\nproperties will be shown too. Defaults to false
.
depth
- tells inspect
how many times to recurse while formatting the\nobject. This is useful for inspecting large complicated objects. Defaults to\n2
. To make it recurse indefinitely pass null
.
colors
- if true
, then the output will be styled with ANSI color codes.\nDefaults to false
. Colors are customizable, see [Customizing\nutil.inspect
colors][].
customInspect
- if false
, then custom inspect(depth, opts)
functions\ndefined on the objects being inspected won't be called. Defaults to true
.
Example of inspecting all properties of the util
object:\n\n
const util = require('util');\n\nconsole.log(util.inspect(util, { showHidden: true, depth: null }));
\nValues may supply their own custom inspect(depth, opts)
functions, when\ncalled they receive the current depth in the recursive inspection, as well as\nthe options object passed to util.inspect()
.\n\n
Color output (if enabled) of util.inspect
is customizable globally\nvia util.inspect.styles
and util.inspect.colors
objects.\n\n
util.inspect.styles
is a map assigning each style a color\nfrom util.inspect.colors
.\nHighlighted styles and their default values are:\n number
(yellow)\n boolean
(yellow)\n string
(green)\n date
(magenta)\n regexp
(red)\n null
(bold)\n undefined
(grey)\n special
- only function at this time (cyan)\n * name
(intentionally no styling)\n\n
Predefined color codes are: white
, grey
, black
, blue
, cyan
,\ngreen
, magenta
, red
and yellow
.\nThere are also bold
, italic
, underline
and inverse
codes.\n\n
Objects also may define their own inspect(depth)
function which util.inspect()
\nwill invoke and use the result of when inspecting the object:\n\n
const util = require('util');\n\nvar obj = { name: 'nate' };\nobj.inspect = function(depth) {\n return `{${this.name}}`;\n};\n\nutil.inspect(obj);\n // "{nate}"
\nYou may also return another Object entirely, and the returned String will be\nformatted according to the returned Object. This is similar to how\nJSON.stringify()
works:\n\n
var obj = { foo: 'this will not show up in the inspect() output' };\nobj.inspect = function(depth) {\n return { bar: 'baz' };\n};\n\nutil.inspect(obj);\n // "{ bar: 'baz' }"
\n"
}
],
"signatures": [
{
"params": [
{
"name": "object"
},
{
"name": "options",
"optional": true
}
]
}
]
},
{
"textRaw": "util.isArray(object)",
"type": "method",
"name": "isArray",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Internal alias for [Array.isArray
][].\n\n
Returns true
if the given "object" is an Array
. Otherwise, returns false
.\n\n
const util = require('util');\n\nutil.isArray([])\n // true\nutil.isArray(new Array)\n // true\nutil.isArray({})\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isBoolean(object)",
"type": "method",
"name": "isBoolean",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is a Boolean
. Otherwise, returns false
.\n\n
const util = require('util');\n\nutil.isBoolean(1)\n // false\nutil.isBoolean(0)\n // false\nutil.isBoolean(false)\n // true
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isBuffer(object)",
"type": "method",
"name": "isBuffer",
"stability": 0,
"stabilityText": "Deprecated: Use [`Buffer.isBuffer()`][] instead.",
"desc": "Returns true
if the given "object" is a Buffer
. Otherwise, returns false
.\n\n
const util = require('util');\n\nutil.isBuffer({ length: 0 })\n // false\nutil.isBuffer([])\n // false\nutil.isBuffer(new Buffer('hello world'))\n // true
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isDate(object)",
"type": "method",
"name": "isDate",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is a Date
. Otherwise, returns false
.\n\n
const util = require('util');\n\nutil.isDate(new Date())\n // true\nutil.isDate(Date())\n // false (without 'new' returns a String)\nutil.isDate({})\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isError(object)",
"type": "method",
"name": "isError",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is an [Error
][]. Otherwise, returns\nfalse
.\n\n
const util = require('util');\n\nutil.isError(new Error())\n // true\nutil.isError(new TypeError())\n // true\nutil.isError({ name: 'Error', message: 'an error occurred' })\n // false
\nNote that this method relies on Object.prototype.toString()
behavior. It is\npossible to obtain an incorrect result when the object
argument manipulates\n@@toStringTag
.\n\n
// This example requires the `--harmony-tostring` flag\nconst util = require('util');\nconst obj = { name: 'Error', message: 'an error occurred' };\n\nutil.isError(obj);\n // false\nobj[Symbol.toStringTag] = 'Error';\nutil.isError(obj);\n // true
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isFunction(object)",
"type": "method",
"name": "isFunction",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is a Function
. Otherwise, returns\nfalse
.\n\n
const util = require('util');\n\nfunction Foo() {}\nvar Bar = function() {};\n\nutil.isFunction({})\n // false\nutil.isFunction(Foo)\n // true\nutil.isFunction(Bar)\n // true
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isNull(object)",
"type": "method",
"name": "isNull",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is strictly null
. Otherwise, returns\nfalse
.\n\n
const util = require('util');\n\nutil.isNull(0)\n // false\nutil.isNull(undefined)\n // false\nutil.isNull(null)\n // true
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isNullOrUndefined(object)",
"type": "method",
"name": "isNullOrUndefined",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is null
or undefined
. Otherwise,\nreturns false
.\n\n
const util = require('util');\n\nutil.isNullOrUndefined(0)\n // false\nutil.isNullOrUndefined(undefined)\n // true\nutil.isNullOrUndefined(null)\n // true
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isNumber(object)",
"type": "method",
"name": "isNumber",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is a Number
. Otherwise, returns false
.\n\n
const util = require('util');\n\nutil.isNumber(false)\n // false\nutil.isNumber(Infinity)\n // true\nutil.isNumber(0)\n // true\nutil.isNumber(NaN)\n // true
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isObject(object)",
"type": "method",
"name": "isObject",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is strictly an Object
and not a\nFunction
. Otherwise, returns false
.\n\n
const util = require('util');\n\nutil.isObject(5)\n // false\nutil.isObject(null)\n // false\nutil.isObject({})\n // true\nutil.isObject(function(){})\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isPrimitive(object)",
"type": "method",
"name": "isPrimitive",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is a primitive type. Otherwise, returns\nfalse
.\n\n
const util = require('util');\n\nutil.isPrimitive(5)\n // true\nutil.isPrimitive('foo')\n // true\nutil.isPrimitive(false)\n // true\nutil.isPrimitive(null)\n // true\nutil.isPrimitive(undefined)\n // true\nutil.isPrimitive({})\n // false\nutil.isPrimitive(function() {})\n // false\nutil.isPrimitive(/^$/)\n // false\nutil.isPrimitive(new Date())\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isRegExp(object)",
"type": "method",
"name": "isRegExp",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is a RegExp
. Otherwise, returns false
.\n\n
const util = require('util');\n\nutil.isRegExp(/some regexp/)\n // true\nutil.isRegExp(new RegExp('another regexp'))\n // true\nutil.isRegExp({})\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isString(object)",
"type": "method",
"name": "isString",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is a String
. Otherwise, returns false
.\n\n
const util = require('util');\n\nutil.isString('')\n // true\nutil.isString('foo')\n // true\nutil.isString(String('foo'))\n // true\nutil.isString(5)\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isSymbol(object)",
"type": "method",
"name": "isSymbol",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is a Symbol
. Otherwise, returns false
.\n\n
const util = require('util');\n\nutil.isSymbol(5)\n // false\nutil.isSymbol('foo')\n // false\nutil.isSymbol(Symbol('foo'))\n // true
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.isUndefined(object)",
"type": "method",
"name": "isUndefined",
"stability": 0,
"stabilityText": "Deprecated",
"desc": "Returns true
if the given "object" is undefined
. Otherwise, returns false
.\n\n
const util = require('util');\n\nvar foo;\nutil.isUndefined(5)\n // false\nutil.isUndefined(foo)\n // true\nutil.isUndefined(null)\n // false
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.log(string)",
"type": "method",
"name": "log",
"stability": 0,
"stabilityText": "Deprecated: Use a third party module instead.",
"desc": "Output with timestamp on stdout
.\n\n
require('util').log('Timestamped message.');
\n",
"signatures": [
{
"params": [
{
"name": "string"
}
]
}
]
},
{
"textRaw": "util.print([...])",
"type": "method",
"name": "print",
"stability": 0,
"stabilityText": "Deprecated: Use [`console.log()`][] instead.",
"desc": "Deprecated predecessor of console.log
.\n\n
Deprecated predecessor of console.log
.\n\n
_extend
was never intended to be used outside of internal NodeJS modules. The\ncommunity found and used it anyway.\n\n
It is deprecated and should not be used in new code. JavaScript comes with very\nsimilar built-in functionality through Object.assign
.\n\n