{ "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

\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

\n", "methods": [ { "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.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\nconverted to strings with util.inspect() and these strings are concatenated,\ndelimited 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.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.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
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

\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
var 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", "desc": "

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

\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

\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

\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

\n

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

\n", "signatures": [ { "params": [ { "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.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 } ] } ] }, { "textRaw": "util.print([...])", "type": "method", "name": "print", "stability": 0, "stabilityText": "Deprecated: Use `console.log` instead.", "desc": "

Deprecated predecessor of console.log.\n\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", "signatures": [ { "params": [ { "name": "readableStream" }, { "name": "writableStream" }, { "name": "callback", "optional": true } ] } ] } ], "type": "module", "displayName": "util" } ] }