{ "source": "doc/api/util.markdown", "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

\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

\n", "methods": [ { "textRaw": "util.debug(string)", "type": "method", "name": "debug", "stability": 0, "stabilityText": "Deprecated: use console.error() instead.", "desc": "

Deprecated predecessor of console.error.\n\n

\n", "signatures": [ { "params": [ { "name": "string" } ] } ] }, { "textRaw": "util.debuglog(section)", "type": "method", "name": "debuglog", "signatures": [ { "return": { "textRaw": "Returns: {Function} The logging function ", "name": "return", "type": "Function", "desc": "The logging function" }, "params": [ { "textRaw": "`section` {String} The section of the program to be debugged ", "name": "section", "type": "String", "desc": "The section of the program to be debugged" } ] }, { "params": [ { "name": "section" } ] } ], "desc": "

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

\n

For example:\n\n

\n
var debuglog = util.debuglog('foo');\n\nvar bar = 123;\ndebuglog('hello from foo [%d]', bar);
\n

If this program is run with NODE_DEBUG=foo in the environment, then\nit will output something like:\n\n

\n
FOO 3245: hello from foo [123]
\n

where 3245 is the process id. If it is not run with that\nenvironment variable set, then it will not print anything.\n\n

\n

You may separate multiple NODE_DEBUG environment variables with a\ncomma. For example, NODE_DEBUG=fs,net,tls.\n\n

\n" }, { "textRaw": "util.deprecate(function, string)", "type": "method", "name": "deprecate", "desc": "

Marks that a method should not be used any more.\n\n

\n
const util = require('util');\n\nexports.puts = util.deprecate(function() {\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');
\n

It returns a modified function which warns once by default.\n\n

\n

If --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

\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

\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

\n

process.throwDeprecation takes precedence over process.traceDeprecation.\n\n

\n", "signatures": [ { "params": [ { "name": "function" }, { "name": "string" } ] } ] }, { "textRaw": "util.error([...])", "type": "method", "name": "error", "stability": 0, "stabilityText": "Deprecated: Use console.error() instead.", "desc": "

Deprecated predecessor of console.error.\n\n

\n", "signatures": [ { "params": [ { "name": "...", "optional": true } ] } ] }, { "textRaw": "util.format(format[, ...])", "type": "method", "name": "format", "desc": "

Returns a formatted string using the first argument as a printf-like format.\n\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\n

If the placeholder does not have a corresponding argument, the placeholder is\nnot replaced.\n\n

\n
util.format('%s:%s', 'foo'); // 'foo:%s'
\n

If 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

\n
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
\n

If 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

\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

\n

As an additional convenience, superConstructor will be accessible\nthrough the constructor.super_ property.\n\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

\n

An optional options object may be passed that alters certain aspects of the\nformatted string:\n\n

\n\n

Example of inspecting all properties of the util object:\n\n

\n
const util = require('util');\n\nconsole.log(util.inspect(util, { showHidden: true, depth: null }));
\n

Values 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

\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

\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

\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

\n" }, { "textRaw": "Custom `inspect()` function on Objects", "name": "Custom `inspect()` function on Objects", "type": "misc", "desc": "

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

\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}"
\n

You 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

\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

\n

Returns true if the given "object" is an Array. false otherwise.\n\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. false otherwise.\n\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", "desc": "

Use Buffer.isBuffer() instead.\n\n

\n

Returns true if the given "object" is a Buffer. false otherwise.\n\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. false otherwise.\n\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][]. false otherwise.\n\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
\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. false otherwise.\n\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. false otherwise.\n\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. false otherwise.\n\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. false otherwise.\n\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. false otherwise.\n\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. false otherwise.\n\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. false otherwise.\n\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. false otherwise.\n\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. false otherwise.\n\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. false otherwise.\n\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", "desc": "

Output with timestamp on stdout.\n\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

\n", "signatures": [ { "params": [ { "name": "...", "optional": true } ] } ] }, { "textRaw": "util.pump(readableStream, writableStream[, callback])", "type": "method", "name": "pump", "stability": 0, "stabilityText": "Deprecated: Use readableStream.pipe(writableStream)", "desc": "

Deprecated predecessor of stream.pipe().\n\n

\n", "signatures": [ { "params": [ { "name": "readableStream" }, { "name": "writableStream" }, { "name": "callback", "optional": true } ] } ] }, { "textRaw": "util.puts([...])", "type": "method", "name": "puts", "stability": 0, "stabilityText": "Deprecated: Use console.log() instead.", "desc": "

Deprecated predecessor of console.log.\n\n

\n", "signatures": [ { "params": [ { "name": "...", "optional": true } ] } ] } ], "type": "module", "displayName": "util" } ] }