{ "source": "doc/api/util.markdown", "modules": [ { "textRaw": "util", "name": "util", "stability": 5, "stabilityText": "Locked", "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'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's internal functionality.\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
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 contains circular references.
\n%
- 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\nconverted to strings with util.inspect()
and these strings are concatenated,\ndelimited 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.log(string)",
"type": "method",
"name": "log",
"desc": "Output with timestamp on stdout
.\n\n
require('util').log('Timestamped message.');
\n",
"signatures": [
{
"params": [
{
"name": "string"
}
]
}
]
},
{
"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 properties will be\nshown 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 below.
customInspect
- if false
, then custom inspect()
functions defined on the\nobjects being inspected won't be called. Defaults to true
.
Example of inspecting all properties of the util
object:\n\n
var util = require('util');\n\nconsole.log(util.inspect(util, { showHidden: true, depth: null }));
\n",
"miscs": [
{
"textRaw": "Customizing `util.inspect` colors",
"name": "Customizing `util.inspect` colors",
"type": "misc",
"desc": "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
var 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",
"desc": "Returns true
if the given "object" is an Array
. false
otherwise.\n\n
var 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.isRegExp(object)",
"type": "method",
"name": "isRegExp",
"desc": "Returns true
if the given "object" is a RegExp
. false
otherwise.\n\n
var 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.isDate(object)",
"type": "method",
"name": "isDate",
"desc": "Returns true
if the given "object" is a Date
. false
otherwise.\n\n
var 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",
"desc": "Returns true
if the given "object" is an Error
. false
otherwise.\n\n
var 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
\n",
"signatures": [
{
"params": [
{
"name": "object"
}
]
}
]
},
{
"textRaw": "util.inherits(constructor, superConstructor)",
"type": "method",
"name": "inherits",
"desc": "Inherit the prototype methods from one\nconstructor\ninto another. The prototype of constructor
will be set to a new\nobject created from superConstructor
.\n\n
As an additional convenience, superConstructor
will be accessible\nthrough the constructor.super_
property.\n\n
var util = require("util");\nvar events = require("events");\n\nfunction MyStream() {\n events.EventEmitter.call(this);\n}\n\nutil.inherits(MyStream, events.EventEmitter);\n\nMyStream.prototype.write = function(data) {\n this.emit("data", data);\n}\n\nvar stream = new MyStream();\n\nconsole.log(stream instanceof events.EventEmitter); // true\nconsole.log(MyStream.super_ === events.EventEmitter); // true\n\nstream.on("data", function(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.debug(string)",
"type": "method",
"name": "debug",
"stability": 0,
"stabilityText": "Deprecated: use console.error() instead.",
"desc": "Deprecated predecessor of console.error
.\n\n
Deprecated predecessor of console.error
.\n\n
Deprecated predecessor of console.log
.\n\n
Deprecated predecessor of console.log
.\n\n\n
Deprecated predecessor of stream.pipe()
.\n