Skip to content

Latest commit

Β 

History

History
1023 lines (774 loc) Β· 24.5 KB

File metadata and controls

1023 lines (774 loc) Β· 24.5 KB
Β 
Feb 10, 2017
Feb 10, 2017
1
# Util
Oct 28, 2010
Oct 28, 2010
2
Aug 4, 2016
Aug 4, 2016
3
> Stability: 2 - Stable
Mar 4, 2012
Mar 4, 2012
4
May 23, 2016
May 23, 2016
5
The `util` module is primarily designed to support the needs of Node.js' own
6
internal APIs. However, many of the utilities are useful for application and
7
module developers as well. It can be accessed using:
Oct 28, 2010
Oct 28, 2010
8
May 23, 2016
May 23, 2016
9
```js
10
const util = require('util');
11
```
Nov 13, 2015
Nov 13, 2015
12
Jun 11, 2017
Jun 11, 2017
13
## util.callbackify(original)
14
<!-- YAML
15
added: REPLACEME
16
-->
17
18
* `original` {Function} An `async` function
19
* Returns: {Function} a callback style function
20
21
Takes an `async` function (or a function that returns a Promise) and returns a
22
function following the Node.js error first callback style. In the callback, the
23
first argument will be the rejection reason (or `null` if the Promise resolved),
24
and the second argument will be the resolved value.
25
26
For example:
27
28
```js
29
const util = require('util');
30
31
async function fn() {
32
return await Promise.resolve('hello world');
33
}
34
const callbackFunction = util.callbackify(fn);
35
36
callbackFunction((err, ret) => {
37
if (err) throw err;
38
console.log(ret);
39
});
40
```
41
42
Will print:
43
44
```txt
45
hello world
46
```
47
48
*Note*:
49
50
* The callback is executed asynchronously, and will have a limited stack trace.
51
If the callback throws, the process will emit an [`'uncaughtException'`][]
52
event, and if not handled will exit.
53
54
* Since `null` has a special meaning as the first argument to a callback, if a
55
wrapped function rejects a `Promise` with a falsy value as a reason, the value
56
is wrapped in an `Error` with the original value stored in a field named
57
`reason`.
58
```js
59
function fn() {
60
return Promise.reject(null);
61
}
62
const callbackFunction = util.callbackify(fn);
63
64
callbackFunction((err, ret) => {
65
// When the Promise was rejected with `null` it is wrapped with an Error and
66
// the original value is stored in `reason`.
67
err && err.hasOwnProperty('reason') && err.reason === null; // true
68
});
69
```
70
May 21, 2013
May 21, 2013
71
## util.debuglog(section)
Aug 23, 2016
Aug 23, 2016
72
<!-- YAML
73
added: v0.11.3
74
-->
May 21, 2013
May 21, 2013
75
Mar 2, 2017
Mar 2, 2017
76
* `section` {string} A string identifying the portion of the application for
May 23, 2016
May 23, 2016
77
which the `debuglog` function is being created.
May 21, 2013
May 21, 2013
78
* Returns: {Function} The logging function
79
May 23, 2016
May 23, 2016
80
The `util.debuglog()` method is used to create a function that conditionally
81
writes debug messages to `stderr` based on the existence of the `NODE_DEBUG`
82
environment variable. If the `section` name appears within the value of that
May 29, 2016
May 29, 2016
83
environment variable, then the returned function operates similar to
Jul 14, 2016
Jul 14, 2016
84
[`console.error()`][]. If not, then the returned function is a no-op.
May 21, 2013
May 21, 2013
85
86
For example:
87
Jan 21, 2016
Jan 21, 2016
88
```js
May 29, 2016
May 29, 2016
89
const util = require('util');
May 23, 2016
May 23, 2016
90
const debuglog = util.debuglog('foo');
May 21, 2013
May 21, 2013
91
May 23, 2016
May 23, 2016
92
debuglog('hello from foo [%d]', 123);
May 21, 2013
May 21, 2013
93
```
94
95
If this program is run with `NODE_DEBUG=foo` in the environment, then
96
it will output something like:
97
Jul 14, 2016
Jul 14, 2016
98
```txt
Jan 21, 2016
Jan 21, 2016
99
FOO 3245: hello from foo [123]
100
```
May 21, 2013
May 21, 2013
101
102
where `3245` is the process id. If it is not run with that
103
environment variable set, then it will not print anything.
104
May 23, 2016
May 23, 2016
105
Multiple comma-separated `section` names may be specified in the `NODE_DEBUG`
106
environment variable. For example: `NODE_DEBUG=fs,net,tls`.
Oct 28, 2010
Oct 28, 2010
107
Nov 13, 2015
Nov 13, 2015
108
## util.deprecate(function, string)
Aug 23, 2016
Aug 23, 2016
109
<!-- YAML
110
added: v0.8.0
111
-->
Nov 13, 2015
Nov 13, 2015
112
Aug 4, 2016
Aug 4, 2016
113
The `util.deprecate()` method wraps the given `function` or class in such a way that
May 29, 2016
May 29, 2016
114
it is marked as deprecated.
Nov 13, 2015
Nov 13, 2015
115
Jun 5, 2017
Jun 5, 2017
116
<!-- eslint-disable prefer-rest-params -->
Jan 21, 2016
Jan 21, 2016
117
```js
118
const util = require('util');
Nov 13, 2015
Nov 13, 2015
119
Jul 13, 2016
Jul 13, 2016
120
exports.puts = util.deprecate(function() {
Apr 24, 2017
Apr 24, 2017
121
for (let i = 0, len = arguments.length; i < len; ++i) {
Jan 21, 2016
Jan 21, 2016
122
process.stdout.write(arguments[i] + '\n');
123
}
124
}, 'util.puts: Use console.log instead');
125
```
Nov 13, 2015
Nov 13, 2015
126
May 29, 2016
May 29, 2016
127
When called, `util.deprecate()` will return a function that will emit a
May 23, 2016
May 23, 2016
128
`DeprecationWarning` using the `process.on('warning')` event. By default,
May 29, 2016
May 29, 2016
129
this warning will be emitted and printed to `stderr` exactly once, the first
130
time it is called. After the warning is emitted, the wrapped `function`
May 23, 2016
May 23, 2016
131
is called.
Nov 13, 2015
Nov 13, 2015
132
May 23, 2016
May 23, 2016
133
If either the `--no-deprecation` or `--no-warnings` command line flags are
134
used, or if the `process.noDeprecation` property is set to `true` *prior* to
135
the first deprecation warning, the `util.deprecate()` method does nothing.
Nov 13, 2015
Nov 13, 2015
136
May 23, 2016
May 23, 2016
137
If the `--trace-deprecation` or `--trace-warnings` command line flags are set,
138
or the `process.traceDeprecation` property is set to `true`, a warning and a
139
stack trace are printed to `stderr` the first time the deprecated function is
140
called.
Nov 13, 2015
Nov 13, 2015
141
May 23, 2016
May 23, 2016
142
If the `--throw-deprecation` command line flag is set, or the
143
`process.throwDeprecation` property is set to `true`, then an exception will be
144
thrown when the deprecated function is called.
Nov 13, 2015
Nov 13, 2015
145
May 23, 2016
May 23, 2016
146
The `--throw-deprecation` command line flag and `process.throwDeprecation`
147
property take precedence over `--trace-deprecation` and
148
`process.traceDeprecation`.
Nov 13, 2015
Nov 13, 2015
149
Sep 14, 2016
Sep 14, 2016
150
## util.format(format[, ...args])
Aug 23, 2016
Aug 23, 2016
151
<!-- YAML
152
added: v0.5.3
153
-->
Aug 5, 2011
Aug 5, 2011
154
Mar 2, 2017
Mar 2, 2017
155
* `format` {string} A `printf`-like format string.
May 23, 2016
May 23, 2016
156
157
The `util.format()` method returns a formatted string using the first argument
158
as a `printf`-like format.
Aug 5, 2011
Aug 5, 2011
159
May 23, 2016
May 23, 2016
160
The first argument is a string containing zero or more *placeholder* tokens.
161
Each placeholder token is replaced with the converted value from the
162
corresponding argument. Supported placeholders are:
Aug 5, 2011
Aug 5, 2011
163
164
* `%s` - String.
Mar 29, 2017
Mar 29, 2017
165
* `%d` - Number (integer or floating point value).
166
* `%i` - Integer.
167
* `%f` - Floating point value.
Jul 29, 2013
Jul 29, 2013
168
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument
Mar 8, 2015
Mar 8, 2015
169
contains circular references.
Aug 5, 2011
Aug 5, 2011
170
* `%%` - single percent sign (`'%'`). This does not consume an argument.
171
Aug 8, 2011
Aug 8, 2011
172
If the placeholder does not have a corresponding argument, the placeholder is
173
not replaced.
Aug 5, 2011
Aug 5, 2011
174
Jan 21, 2016
Jan 21, 2016
175
```js
May 23, 2016
May 23, 2016
176
util.format('%s:%s', 'foo');
Nov 16, 2016
Nov 16, 2016
177
// Returns: 'foo:%s'
Jan 21, 2016
Jan 21, 2016
178
```
Aug 5, 2011
Aug 5, 2011
179
May 29, 2016
May 29, 2016
180
If there are more arguments passed to the `util.format()` method than the
181
number of placeholders, the extra arguments are coerced into strings (for
182
objects and symbols, `util.inspect()` is used) then concatenated to the
183
returned string, each delimited by a space.
Aug 5, 2011
Aug 5, 2011
184
Jan 21, 2016
Jan 21, 2016
185
```js
186
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
187
```
Aug 5, 2011
Aug 5, 2011
188
189
If the first argument is not a format string then `util.format()` returns
May 23, 2016
May 23, 2016
190
a string that is the concatenation of all arguments separated by spaces.
191
Each argument is converted to a string using `util.inspect()`.
Aug 5, 2011
Aug 5, 2011
192
Jan 21, 2016
Jan 21, 2016
193
```js
194
util.format(1, 2, 3); // '1 2 3'
195
```
Aug 5, 2011
Aug 5, 2011
196
Apr 13, 2017
Apr 13, 2017
197
If only one argument is passed to `util.format()`, it is returned as it is
198
without any formatting.
199
200
```js
201
util.format('%% %s'); // '%% %s'
202
```
203
Nov 13, 2015
Nov 13, 2015
204
## util.inherits(constructor, superConstructor)
Aug 23, 2016
Aug 23, 2016
205
<!-- YAML
206
added: v0.3.0
Feb 24, 2017
Feb 24, 2017
207
changes:
208
- version: v5.0.0
209
pr-url: https://github.com/nodejs/node/pull/3455
210
description: The `constructor` parameter can refer to an ES6 class now.
Aug 23, 2016
Aug 23, 2016
211
-->
Aug 5, 2011
Aug 5, 2011
212
May 25, 2017
May 25, 2017
213
*Note*: Usage of `util.inherits()` is discouraged. Please use the ES6 `class`
214
and `extends` keywords to get language level inheritance support. Also note
215
that the two styles are [semantically incompatible][].
May 9, 2016
May 9, 2016
216
May 23, 2016
May 23, 2016
217
* `constructor` {Function}
218
* `superConstructor` {Function}
219
Nov 16, 2015
Nov 16, 2015
220
Inherit the prototype methods from one [constructor][] into another. The
221
prototype of `constructor` will be set to a new object created from
222
`superConstructor`.
Oct 28, 2010
Oct 28, 2010
223
Nov 13, 2015
Nov 13, 2015
224
As an additional convenience, `superConstructor` will be accessible
225
through the `constructor.super_` property.
Oct 28, 2010
Oct 28, 2010
226
Jan 21, 2016
Jan 21, 2016
227
```js
228
const util = require('util');
229
const EventEmitter = require('events');
Nov 13, 2015
Nov 13, 2015
230
Jan 21, 2016
Jan 21, 2016
231
function MyStream() {
Jul 15, 2016
Jul 15, 2016
232
EventEmitter.call(this);
Jan 21, 2016
Jan 21, 2016
233
}
Nov 13, 2015
Nov 13, 2015
234
Jan 21, 2016
Jan 21, 2016
235
util.inherits(MyStream, EventEmitter);
Nov 13, 2015
Nov 13, 2015
236
Jan 21, 2016
Jan 21, 2016
237
MyStream.prototype.write = function(data) {
Jul 15, 2016
Jul 15, 2016
238
this.emit('data', data);
239
};
Nov 13, 2015
Nov 13, 2015
240
May 29, 2016
May 29, 2016
241
const stream = new MyStream();
Nov 13, 2015
Nov 13, 2015
242
Jan 21, 2016
Jan 21, 2016
243
console.log(stream instanceof EventEmitter); // true
244
console.log(MyStream.super_ === EventEmitter); // true
Nov 13, 2015
Nov 13, 2015
245
Jan 21, 2016
Jan 21, 2016
246
stream.on('data', (data) => {
247
console.log(`Received data: "${data}"`);
Jul 15, 2016
Jul 15, 2016
248
});
Jan 21, 2016
Jan 21, 2016
249
stream.write('It works!'); // Received data: "It works!"
250
```
Oct 28, 2010
Oct 28, 2010
251
Aug 21, 2016
Aug 21, 2016
252
ES6 example using `class` and `extends`
253
254
```js
255
const EventEmitter = require('events');
256
257
class MyStream extends EventEmitter {
258
write(data) {
259
this.emit('data', data);
260
}
261
}
262
263
const stream = new MyStream();
264
265
stream.on('data', (data) => {
266
console.log(`Received data: "${data}"`);
267
});
268
stream.write('With ES6');
269
270
```
271
Sep 25, 2014
Sep 25, 2014
272
## util.inspect(object[, options])
Aug 23, 2016
Aug 23, 2016
273
<!-- YAML
274
added: v0.3.0
Feb 24, 2017
Feb 24, 2017
275
changes:
276
- version: v6.6.0
277
pr-url: https://github.com/nodejs/node/pull/8174
278
description: Custom inspection functions can now return `this`.
279
- version: v6.3.0
280
pr-url: https://github.com/nodejs/node/pull/7499
281
description: The `breakLength` option is supported now.
282
- version: v6.1.0
283
pr-url: https://github.com/nodejs/node/pull/6334
284
description: The `maxArrayLength` option is supported now; in particular,
285
long arrays are truncated by default.
286
- version: v6.1.0
287
pr-url: https://github.com/nodejs/node/pull/6465
288
description: The `showProxy` option is supported now.
Aug 23, 2016
Aug 23, 2016
289
-->
Oct 28, 2010
Oct 28, 2010
290
May 23, 2016
May 23, 2016
291
* `object` {any} Any JavaScript primitive or Object.
292
* `options` {Object}
May 29, 2016
May 29, 2016
293
* `showHidden` {boolean} If `true`, the `object`'s non-enumerable symbols and
294
properties will be included in the formatted result. Defaults to `false`.
295
* `depth` {number} Specifies the number of times to recurse while formatting
296
the `object`. This is useful for inspecting large complicated objects.
297
Defaults to `2`. To make it recurse indefinitely pass `null`.
May 23, 2016
May 23, 2016
298
* `colors` {boolean} If `true`, the output will be styled with ANSI color
299
codes. Defaults to `false`. Colors are customizable, see
300
[Customizing `util.inspect` colors][].
301
* `customInspect` {boolean} If `false`, then custom `inspect(depth, opts)`
302
functions exported on the `object` being inspected will not be called.
303
Defaults to `true`.
304
* `showProxy` {boolean} If `true`, then objects and functions that are
May 29, 2016
May 29, 2016
305
`Proxy` objects will be introspected to show their `target` and `handler`
May 23, 2016
May 23, 2016
306
objects. Defaults to `false`.
307
* `maxArrayLength` {number} Specifies the maximum number of array and
308
`TypedArray` elements to include when formatting. Defaults to `100`. Set to
309
`null` to show all array elements. Set to `0` or negative to show no array
310
elements.
Jul 5, 2016
Jul 5, 2016
311
* `breakLength` {number} The length at which an object's keys are split
312
across multiple lines. Set to `Infinity` to format an object as a single
313
line. Defaults to 60 for legacy compatibility.
May 23, 2016
May 23, 2016
314
315
The `util.inspect()` method returns a string representation of `object` that is
316
primarily useful for debugging. Additional `options` may be passed that alter
317
certain aspects of the formatted string.
318
319
The following example inspects all properties of the `util` object:
Oct 28, 2010
Oct 28, 2010
320
Jan 21, 2016
Jan 21, 2016
321
```js
322
const util = require('util');
Oct 28, 2010
Oct 28, 2010
323
Jan 21, 2016
Jan 21, 2016
324
console.log(util.inspect(util, { showHidden: true, depth: null }));
325
```
Oct 28, 2010
Oct 28, 2010
326
Aug 22, 2013
Aug 22, 2013
327
Values may supply their own custom `inspect(depth, opts)` functions, when
May 23, 2016
May 23, 2016
328
called these receive the current `depth` in the recursive inspection, as well as
Aug 22, 2013
Aug 22, 2013
329
the options object passed to `util.inspect()`.
330
Jul 17, 2012
Jul 17, 2012
331
### Customizing `util.inspect` colors
332
May 21, 2013
May 21, 2013
333
<!-- type=misc -->
334
Jul 17, 2012
Jul 17, 2012
335
Color output (if enabled) of `util.inspect` is customizable globally
May 23, 2016
May 23, 2016
336
via the `util.inspect.styles` and `util.inspect.colors` properties.
337
338
`util.inspect.styles` is a map associating a style name to a color from
339
`util.inspect.colors`.
340
341
The default styles and associated colors are:
342
343
* `number` - `yellow`
344
* `boolean` - `yellow`
345
* `string` - `green`
346
* `date` - `magenta`
347
* `regexp` - `red`
348
* `null` - `bold`
349
* `undefined` - `grey`
350
* `special` - `cyan` (only applied to functions at this time)
351
* `name` - (no styling)
352
353
The predefined color codes are: `white`, `grey`, `black`, `blue`, `cyan`,
354
`green`, `magenta`, `red` and `yellow`. There are also `bold`, `italic`,
355
`underline` and `inverse` codes.
356
357
Color styling uses ANSI control codes that may not be supported on all
358
terminals.
Jul 17, 2012
Jul 17, 2012
359
Aug 25, 2016
Aug 25, 2016
360
### Custom inspection functions on Objects
Mar 12, 2013
Mar 12, 2013
361
May 21, 2013
May 21, 2013
362
<!-- type=misc -->
363
Aug 25, 2016
Aug 25, 2016
364
Objects may also define their own `[util.inspect.custom](depth, opts)`
365
(or, equivalently `inspect(depth, opts)`) function that `util.inspect()` will
366
invoke and use the result of when inspecting the object:
Oct 5, 2012
Oct 5, 2012
367
Jan 21, 2016
Jan 21, 2016
368
```js
369
const util = require('util');
Oct 5, 2012
Oct 5, 2012
370
Oct 26, 2016
Oct 26, 2016
371
class Box {
372
constructor(value) {
373
this.value = value;
374
}
375
376
inspect(depth, options) {
377
if (depth < 0) {
378
return options.stylize('[Box]', 'special');
379
}
380
381
const newOptions = Object.assign({}, options, {
382
depth: options.depth === null ? null : options.depth - 1
383
});
384
385
// Five space padding because that's the size of "Box< ".
386
const padding = ' '.repeat(5);
Apr 24, 2017
Apr 24, 2017
387
const inner = util.inspect(this.value, newOptions)
Jun 1, 2017
Jun 1, 2017
388
.replace(/\n/g, `\n${padding}`);
389
return `${options.stylize('Box', 'special')}< ${inner} >`;
Oct 26, 2016
Oct 26, 2016
390
}
391
}
392
393
const box = new Box(true);
Oct 5, 2012
Oct 5, 2012
394
Oct 26, 2016
Oct 26, 2016
395
util.inspect(box);
Nov 16, 2016
Nov 16, 2016
396
// Returns: "Box< true >"
Jan 21, 2016
Jan 21, 2016
397
```
Oct 5, 2012
Oct 5, 2012
398
Aug 25, 2016
Aug 25, 2016
399
Custom `[util.inspect.custom](depth, opts)` functions typically return a string
400
but may return a value of any type that will be formatted accordingly by
May 23, 2016
May 23, 2016
401
`util.inspect()`.
Mar 12, 2013
Mar 12, 2013
402
Jan 21, 2016
Jan 21, 2016
403
```js
May 29, 2016
May 29, 2016
404
const util = require('util');
405
Aug 25, 2016
Aug 25, 2016
406
const obj = { foo: 'this will not show up in the inspect() output' };
407
obj[util.inspect.custom] = function(depth) {
408
return { bar: 'baz' };
409
};
410
411
util.inspect(obj);
Nov 16, 2016
Nov 16, 2016
412
// Returns: "{ bar: 'baz' }"
Aug 25, 2016
Aug 25, 2016
413
```
414
415
A custom inspection method can alternatively be provided by exposing
416
an `inspect(depth, opts)` method on the object:
417
418
```js
419
const util = require('util');
420
May 29, 2016
May 29, 2016
421
const obj = { foo: 'this will not show up in the inspect() output' };
Jan 21, 2016
Jan 21, 2016
422
obj.inspect = function(depth) {
423
return { bar: 'baz' };
424
};
Mar 12, 2013
Mar 12, 2013
425
Jan 21, 2016
Jan 21, 2016
426
util.inspect(obj);
Nov 16, 2016
Nov 16, 2016
427
// Returns: "{ bar: 'baz' }"
Jan 21, 2016
Jan 21, 2016
428
```
Mar 12, 2013
Mar 12, 2013
429
Feb 20, 2017
Feb 20, 2017
430
### util.inspect.custom
431
<!-- YAML
432
added: v6.6.0
433
-->
434
435
A Symbol that can be used to declare custom inspect functions, see
436
[Custom inspection functions on Objects][].
437
Aug 9, 2016
Aug 9, 2016
438
### util.inspect.defaultOptions
Aug 23, 2016
Aug 23, 2016
439
<!-- YAML
440
added: v6.4.0
441
-->
Aug 9, 2016
Aug 9, 2016
442
443
The `defaultOptions` value allows customization of the default options used by
444
`util.inspect`. This is useful for functions like `console.log` or
445
`util.format` which implicitly call into `util.inspect`. It shall be set to an
446
object containing one or more valid [`util.inspect()`][] options. Setting
447
option properties directly is also supported.
448
449
```js
450
const util = require('util');
Jun 1, 2017
Jun 1, 2017
451
const arr = Array(101).fill(0);
Aug 9, 2016
Aug 9, 2016
452
453
console.log(arr); // logs the truncated array
454
util.inspect.defaultOptions.maxArrayLength = null;
455
console.log(arr); // logs the full array
456
```
457
May 9, 2017
May 9, 2017
458
## util.promisify(original)
459
<!-- YAML
May 30, 2017
May 30, 2017
460
added: v8.0.0
May 9, 2017
May 9, 2017
461
-->
462
463
* `original` {Function}
464
465
Takes a function following the common Node.js callback style, i.e. taking a
466
`(err, value) => ...` callback as the last argument, and returns a version
467
that returns promises.
468
469
For example:
470
471
```js
472
const util = require('util');
473
const fs = require('fs');
474
475
const stat = util.promisify(fs.stat);
476
stat('.').then((stats) => {
477
// Do something with `stats`
478
}).catch((error) => {
479
// Handle the error.
480
});
481
```
482
483
Or, equivalently using `async function`s:
484
485
```js
486
const util = require('util');
487
const fs = require('fs');
488
489
const stat = util.promisify(fs.stat);
490
491
async function callStat() {
492
const stats = await stat('.');
493
console.log(`This directory is owned by ${stats.uid}`);
494
}
495
```
496
497
If there is an `original[util.promisify.custom]` property present, `promisify`
498
will return its value, see [Custom promisified functions][].
499
500
`promisify()` assumes that `original` is a function taking a callback as its
501
final argument in all cases, and the returned function will result in undefined
May 28, 2017
May 28, 2017
502
behavior if it does not.
May 9, 2017
May 9, 2017
503
504
### Custom promisified functions
505
506
Using the `util.promisify.custom` symbol one can override the return value of
507
[`util.promisify()`][]:
508
509
```js
510
const util = require('util');
511
512
function doSomething(foo, callback) {
513
// ...
514
}
515
516
doSomething[util.promisify.custom] = function(foo) {
517
return getPromiseSomehow();
518
};
519
520
const promisified = util.promisify(doSomething);
521
console.log(promisified === doSomething[util.promisify.custom]);
522
// prints 'true'
523
```
524
525
This can be useful for cases where the original function does not follow the
526
standard format of taking an error-first callback as the last argument.
527
528
### util.promisify.custom
529
<!-- YAML
May 30, 2017
May 30, 2017
530
added: v8.0.0
May 9, 2017
May 9, 2017
531
-->
532
533
* {symbol}
534
535
A Symbol that can be used to declare custom promisified variants of functions,
536
see [Custom promisified functions][].
537
Feb 20, 2017
Feb 20, 2017
538
## Deprecated APIs
539
540
The following APIs have been deprecated and should no longer be used. Existing
541
applications and modules should be updated to find alternative approaches.
542
543
### util.\_extend(target, source)
Aug 25, 2016
Aug 25, 2016
544
<!-- YAML
Feb 20, 2017
Feb 20, 2017
545
added: v0.7.5
546
deprecated: v6.0.0
Aug 25, 2016
Aug 25, 2016
547
-->
548
Feb 20, 2017
Feb 20, 2017
549
> Stability: 0 - Deprecated: Use [`Object.assign()`] instead.
Aug 25, 2016
Aug 25, 2016
550
Feb 20, 2017
Feb 20, 2017
551
The `util._extend()` method was never intended to be used outside of internal
552
Node.js modules. The community found and used it anyway.
May 23, 2016
May 23, 2016
553
Feb 20, 2017
Feb 20, 2017
554
It is deprecated and should not be used in new code. JavaScript comes with very
555
similar built-in functionality through [`Object.assign()`].
May 23, 2016
May 23, 2016
556
557
### util.debug(string)
Aug 23, 2016
Aug 23, 2016
558
<!-- YAML
559
added: v0.3.0
560
deprecated: v0.11.3
561
-->
May 23, 2016
May 23, 2016
562
Aug 4, 2016
Aug 4, 2016
563
> Stability: 0 - Deprecated: Use [`console.error()`][] instead.
May 23, 2016
May 23, 2016
564
Mar 2, 2017
Mar 2, 2017
565
* `string` {string} The message to print to `stderr`
May 23, 2016
May 23, 2016
566
567
Deprecated predecessor of `console.error`.
568
Sep 14, 2016
Sep 14, 2016
569
### util.error([...strings])
Aug 23, 2016
Aug 23, 2016
570
<!-- YAML
571
added: v0.3.0
572
deprecated: v0.11.3
573
-->
May 23, 2016
May 23, 2016
574
Aug 4, 2016
Aug 4, 2016
575
> Stability: 0 - Deprecated: Use [`console.error()`][] instead.
May 23, 2016
May 23, 2016
576
Mar 2, 2017
Mar 2, 2017
577
* `...strings` {string} The message to print to `stderr`
May 23, 2016
May 23, 2016
578
579
Deprecated predecessor of `console.error`.
580
581
### util.isArray(object)
Aug 23, 2016
Aug 23, 2016
582
<!-- YAML
583
added: v0.6.0
584
deprecated: v4.0.0
585
-->
Oct 26, 2011
Oct 26, 2011
586
Aug 4, 2016
Aug 4, 2016
587
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
588
May 23, 2016
May 23, 2016
589
* `object` {any}
590
Dec 3, 2015
Dec 3, 2015
591
Internal alias for [`Array.isArray`][].
Apr 3, 2014
Apr 3, 2014
592
May 23, 2016
May 23, 2016
593
Returns `true` if the given `object` is an `Array`. Otherwise, returns `false`.
Oct 26, 2011
Oct 26, 2011
594
Jan 21, 2016
Jan 21, 2016
595
```js
596
const util = require('util');
Oct 26, 2011
Oct 26, 2011
597
May 29, 2016
May 29, 2016
598
util.isArray([]);
Nov 16, 2016
Nov 16, 2016
599
// Returns: true
Apr 24, 2017
Apr 24, 2017
600
util.isArray(new Array());
Nov 16, 2016
Nov 16, 2016
601
// Returns: true
May 29, 2016
May 29, 2016
602
util.isArray({});
Nov 16, 2016
Nov 16, 2016
603
// Returns: false
Jan 21, 2016
Jan 21, 2016
604
```
Oct 26, 2011
Oct 26, 2011
605
May 23, 2016
May 23, 2016
606
### util.isBoolean(object)
Aug 23, 2016
Aug 23, 2016
607
<!-- YAML
608
added: v0.11.5
609
deprecated: v4.0.0
610
-->
Oct 26, 2011
Oct 26, 2011
611
Aug 4, 2016
Aug 4, 2016
612
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
613
May 23, 2016
May 23, 2016
614
* `object` {any}
615
616
Returns `true` if the given `object` is a `Boolean`. Otherwise, returns `false`.
Oct 26, 2011
Oct 26, 2011
617
Jan 21, 2016
Jan 21, 2016
618
```js
619
const util = require('util');
Oct 26, 2011
Oct 26, 2011
620
May 29, 2016
May 29, 2016
621
util.isBoolean(1);
Nov 16, 2016
Nov 16, 2016
622
// Returns: false
May 29, 2016
May 29, 2016
623
util.isBoolean(0);
Nov 16, 2016
Nov 16, 2016
624
// Returns: false
May 29, 2016
May 29, 2016
625
util.isBoolean(false);
Nov 16, 2016
Nov 16, 2016
626
// Returns: true
Jan 21, 2016
Jan 21, 2016
627
```
Dec 9, 2015
Dec 9, 2015
628
May 23, 2016
May 23, 2016
629
### util.isBuffer(object)
Aug 23, 2016
Aug 23, 2016
630
<!-- YAML
631
added: v0.11.5
632
deprecated: v4.0.0
633
-->
Nov 13, 2015
Nov 13, 2015
634
Aug 4, 2016
Aug 4, 2016
635
> Stability: 0 - Deprecated: Use [`Buffer.isBuffer()`][] instead.
Nov 13, 2015
Nov 13, 2015
636
May 23, 2016
May 23, 2016
637
* `object` {any}
638
639
Returns `true` if the given `object` is a `Buffer`. Otherwise, returns `false`.
Nov 13, 2015
Nov 13, 2015
640
Jan 21, 2016
Jan 21, 2016
641
```js
642
const util = require('util');
Nov 13, 2015
Nov 13, 2015
643
May 29, 2016
May 29, 2016
644
util.isBuffer({ length: 0 });
Nov 16, 2016
Nov 16, 2016
645
// Returns: false
May 29, 2016
May 29, 2016
646
util.isBuffer([]);
Nov 16, 2016
Nov 16, 2016
647
// Returns: false
May 29, 2016
May 29, 2016
648
util.isBuffer(Buffer.from('hello world'));
Nov 16, 2016
Nov 16, 2016
649
// Returns: true
Jan 21, 2016
Jan 21, 2016
650
```
Oct 26, 2011
Oct 26, 2011
651
May 23, 2016
May 23, 2016
652
### util.isDate(object)
Aug 23, 2016
Aug 23, 2016
653
<!-- YAML
654
added: v0.6.0
655
deprecated: v4.0.0
656
-->
Oct 26, 2011
Oct 26, 2011
657
Aug 4, 2016
Aug 4, 2016
658
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
659
May 23, 2016
May 23, 2016
660
* `object` {any}
661
662
Returns `true` if the given `object` is a `Date`. Otherwise, returns `false`.
Oct 26, 2011
Oct 26, 2011
663
Jan 21, 2016
Jan 21, 2016
664
```js
665
const util = require('util');
Oct 26, 2011
Oct 26, 2011
666
May 29, 2016
May 29, 2016
667
util.isDate(new Date());
Nov 16, 2016
Nov 16, 2016
668
// Returns: true
May 29, 2016
May 29, 2016
669
util.isDate(Date());
Nov 16, 2016
Nov 16, 2016
670
// false (without 'new' returns a String)
May 29, 2016
May 29, 2016
671
util.isDate({});
Nov 16, 2016
Nov 16, 2016
672
// Returns: false
Jan 21, 2016
Jan 21, 2016
673
```
Oct 26, 2011
Oct 26, 2011
674
May 23, 2016
May 23, 2016
675
### util.isError(object)
Aug 23, 2016
Aug 23, 2016
676
<!-- YAML
677
added: v0.6.0
678
deprecated: v4.0.0
679
-->
Oct 26, 2011
Oct 26, 2011
680
Aug 4, 2016
Aug 4, 2016
681
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
682
May 23, 2016
May 23, 2016
683
* `object` {any}
684
685
Returns `true` if the given `object` is an [`Error`][]. Otherwise, returns
Feb 25, 2016
Feb 25, 2016
686
`false`.
Oct 26, 2011
Oct 26, 2011
687
Jan 21, 2016
Jan 21, 2016
688
```js
689
const util = require('util');
Oct 26, 2011
Oct 26, 2011
690
May 29, 2016
May 29, 2016
691
util.isError(new Error());
Nov 16, 2016
Nov 16, 2016
692
// Returns: true
May 29, 2016
May 29, 2016
693
util.isError(new TypeError());
Nov 16, 2016
Nov 16, 2016
694
// Returns: true
May 29, 2016
May 29, 2016
695
util.isError({ name: 'Error', message: 'an error occurred' });
Nov 16, 2016
Nov 16, 2016
696
// Returns: false
Jan 21, 2016
Jan 21, 2016
697
```
Oct 26, 2011
Oct 26, 2011
698
Feb 25, 2016
Feb 25, 2016
699
Note that this method relies on `Object.prototype.toString()` behavior. It is
700
possible to obtain an incorrect result when the `object` argument manipulates
May 31, 2016
May 31, 2016
701
`@@toStringTag`.
Feb 25, 2016
Feb 25, 2016
702
703
```js
704
const util = require('util');
705
const obj = { name: 'Error', message: 'an error occurred' };
706
707
util.isError(obj);
Nov 16, 2016
Nov 16, 2016
708
// Returns: false
Feb 25, 2016
Feb 25, 2016
709
obj[Symbol.toStringTag] = 'Error';
710
util.isError(obj);
Nov 16, 2016
Nov 16, 2016
711
// Returns: true
Feb 25, 2016
Feb 25, 2016
712
```
713
May 23, 2016
May 23, 2016
714
### util.isFunction(object)
Aug 23, 2016
Aug 23, 2016
715
<!-- YAML
716
added: v0.11.5
717
deprecated: v4.0.0
718
-->
Jan 14, 2015
Jan 14, 2015
719
Aug 4, 2016
Aug 4, 2016
720
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
721
May 23, 2016
May 23, 2016
722
* `object` {any}
723
724
Returns `true` if the given `object` is a `Function`. Otherwise, returns
Feb 25, 2016
Feb 25, 2016
725
`false`.
Jan 14, 2015
Jan 14, 2015
726
Jan 21, 2016
Jan 21, 2016
727
```js
728
const util = require('util');
Jan 14, 2015
Jan 14, 2015
729
Jan 21, 2016
Jan 21, 2016
730
function Foo() {}
May 1, 2017
May 1, 2017
731
const Bar = () => {};
Nov 13, 2015
Nov 13, 2015
732
May 29, 2016
May 29, 2016
733
util.isFunction({});
Nov 16, 2016
Nov 16, 2016
734
// Returns: false
May 29, 2016
May 29, 2016
735
util.isFunction(Foo);
Nov 16, 2016
Nov 16, 2016
736
// Returns: true
May 29, 2016
May 29, 2016
737
util.isFunction(Bar);
Nov 16, 2016
Nov 16, 2016
738
// Returns: true
Jan 21, 2016
Jan 21, 2016
739
```
Jan 14, 2015
Jan 14, 2015
740
May 23, 2016
May 23, 2016
741
### util.isNull(object)
Aug 23, 2016
Aug 23, 2016
742
<!-- YAML
743
added: v0.11.5
744
deprecated: v4.0.0
745
-->
Jan 14, 2015
Jan 14, 2015
746
Aug 4, 2016
Aug 4, 2016
747
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
748
May 23, 2016
May 23, 2016
749
* `object` {any}
750
751
Returns `true` if the given `object` is strictly `null`. Otherwise, returns
Feb 25, 2016
Feb 25, 2016
752
`false`.
Jan 14, 2015
Jan 14, 2015
753
Jan 21, 2016
Jan 21, 2016
754
```js
755
const util = require('util');
Jan 14, 2015
Jan 14, 2015
756
May 29, 2016
May 29, 2016
757
util.isNull(0);
Nov 16, 2016
Nov 16, 2016
758
// Returns: false
May 29, 2016
May 29, 2016
759
util.isNull(undefined);
Nov 16, 2016
Nov 16, 2016
760
// Returns: false
May 29, 2016
May 29, 2016
761
util.isNull(null);
Nov 16, 2016
Nov 16, 2016
762
// Returns: true
Jan 21, 2016
Jan 21, 2016
763
```
Jan 14, 2015
Jan 14, 2015
764
May 23, 2016
May 23, 2016
765
### util.isNullOrUndefined(object)
Aug 23, 2016
Aug 23, 2016
766
<!-- YAML
767
added: v0.11.5
768
deprecated: v4.0.0
769
-->
Jan 14, 2015
Jan 14, 2015
770
Aug 4, 2016
Aug 4, 2016
771
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
772
May 23, 2016
May 23, 2016
773
* `object` {any}
774
775
Returns `true` if the given `object` is `null` or `undefined`. Otherwise,
Feb 25, 2016
Feb 25, 2016
776
returns `false`.
Jan 14, 2015
Jan 14, 2015
777
Jan 21, 2016
Jan 21, 2016
778
```js
779
const util = require('util');
Jan 14, 2015
Jan 14, 2015
780
May 29, 2016
May 29, 2016
781
util.isNullOrUndefined(0);
Nov 16, 2016
Nov 16, 2016
782
// Returns: false
May 29, 2016
May 29, 2016
783
util.isNullOrUndefined(undefined);
Nov 16, 2016
Nov 16, 2016
784
// Returns: true
May 29, 2016
May 29, 2016
785
util.isNullOrUndefined(null);
Nov 16, 2016
Nov 16, 2016
786
// Returns: true
Jan 21, 2016
Jan 21, 2016
787
```
Jan 14, 2015
Jan 14, 2015
788
May 23, 2016
May 23, 2016
789
### util.isNumber(object)
Aug 23, 2016
Aug 23, 2016
790
<!-- YAML
791
added: v0.11.5
792
deprecated: v4.0.0
793
-->
Jan 14, 2015
Jan 14, 2015
794
Aug 4, 2016
Aug 4, 2016
795
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
796
May 23, 2016
May 23, 2016
797
* `object` {any}
798
799
Returns `true` if the given `object` is a `Number`. Otherwise, returns `false`.
Jan 14, 2015
Jan 14, 2015
800
Jan 21, 2016
Jan 21, 2016
801
```js
802
const util = require('util');
803
May 29, 2016
May 29, 2016
804
util.isNumber(false);
Nov 16, 2016
Nov 16, 2016
805
// Returns: false
May 29, 2016
May 29, 2016
806
util.isNumber(Infinity);
Nov 16, 2016
Nov 16, 2016
807
// Returns: true
May 29, 2016
May 29, 2016
808
util.isNumber(0);
Nov 16, 2016
Nov 16, 2016
809
// Returns: true
May 29, 2016
May 29, 2016
810
util.isNumber(NaN);
Nov 16, 2016
Nov 16, 2016
811
// Returns: true
Jan 21, 2016
Jan 21, 2016
812
```
Jan 14, 2015
Jan 14, 2015
813
May 23, 2016
May 23, 2016
814
### util.isObject(object)
Aug 23, 2016
Aug 23, 2016
815
<!-- YAML
816
added: v0.11.5
817
deprecated: v4.0.0
818
-->
Jan 14, 2015
Jan 14, 2015
819
Aug 4, 2016
Aug 4, 2016
820
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
821
May 23, 2016
May 23, 2016
822
* `object` {any}
823
Sep 25, 2016
Sep 25, 2016
824
Returns `true` if the given `object` is strictly an `Object` **and** not a
Feb 25, 2016
Feb 25, 2016
825
`Function`. Otherwise, returns `false`.
Jan 14, 2015
Jan 14, 2015
826
Jan 21, 2016
Jan 21, 2016
827
```js
828
const util = require('util');
829
May 29, 2016
May 29, 2016
830
util.isObject(5);
Nov 16, 2016
Nov 16, 2016
831
// Returns: false
May 29, 2016
May 29, 2016
832
util.isObject(null);
Nov 16, 2016
Nov 16, 2016
833
// Returns: false
May 29, 2016
May 29, 2016
834
util.isObject({});
Nov 16, 2016
Nov 16, 2016
835
// Returns: true
Apr 24, 2017
Apr 24, 2017
836
util.isObject(function() {});
Nov 16, 2016
Nov 16, 2016
837
// Returns: false
Jan 21, 2016
Jan 21, 2016
838
```
Jan 14, 2015
Jan 14, 2015
839
May 23, 2016
May 23, 2016
840
### util.isPrimitive(object)
Aug 23, 2016
Aug 23, 2016
841
<!-- YAML
842
added: v0.11.5
843
deprecated: v4.0.0
844
-->
Jan 14, 2015
Jan 14, 2015
845
Aug 4, 2016
Aug 4, 2016
846
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
847
May 23, 2016
May 23, 2016
848
* `object` {any}
849
850
Returns `true` if the given `object` is a primitive type. Otherwise, returns
Feb 25, 2016
Feb 25, 2016
851
`false`.
Jan 14, 2015
Jan 14, 2015
852
Jan 21, 2016
Jan 21, 2016
853
```js
854
const util = require('util');
855
May 29, 2016
May 29, 2016
856
util.isPrimitive(5);
Nov 16, 2016
Nov 16, 2016
857
// Returns: true
May 29, 2016
May 29, 2016
858
util.isPrimitive('foo');
Nov 16, 2016
Nov 16, 2016
859
// Returns: true
May 29, 2016
May 29, 2016
860
util.isPrimitive(false);
Nov 16, 2016
Nov 16, 2016
861
// Returns: true
May 29, 2016
May 29, 2016
862
util.isPrimitive(null);
Nov 16, 2016
Nov 16, 2016
863
// Returns: true
May 29, 2016
May 29, 2016
864
util.isPrimitive(undefined);
Nov 16, 2016
Nov 16, 2016
865
// Returns: true
May 29, 2016
May 29, 2016
866
util.isPrimitive({});
Nov 16, 2016
Nov 16, 2016
867
// Returns: false
May 29, 2016
May 29, 2016
868
util.isPrimitive(function() {});
Nov 16, 2016
Nov 16, 2016
869
// Returns: false
May 29, 2016
May 29, 2016
870
util.isPrimitive(/^$/);
Nov 16, 2016
Nov 16, 2016
871
// Returns: false
May 29, 2016
May 29, 2016
872
util.isPrimitive(new Date());
Nov 16, 2016
Nov 16, 2016
873
// Returns: false
Jan 21, 2016
Jan 21, 2016
874
```
Jan 14, 2015
Jan 14, 2015
875
May 23, 2016
May 23, 2016
876
### util.isRegExp(object)
Aug 23, 2016
Aug 23, 2016
877
<!-- YAML
878
added: v0.6.0
879
deprecated: v4.0.0
880
-->
Jan 14, 2015
Jan 14, 2015
881
Aug 4, 2016
Aug 4, 2016
882
> Stability: 0 - Deprecated
Aug 24, 2015
Aug 24, 2015
883
May 23, 2016
May 23, 2016
884
* `object` {any}
885
886
Returns `true` if the given `object` is a `RegExp`. Otherwise, returns `false`.
Jan 14, 2015
Jan 14, 2015
887
Jan 21, 2016
Jan 21, 2016
888
```js
889
const util = require('util');
Jan 14, 2015
Jan 14, 2015
890
May 29, 2016
May 29, 2016
891
util.isRegExp(/some regexp/);
Nov 16, 2016
Nov 16, 2016
892
// Returns: true
May 29, 2016
May 29, 2016
893
util.isRegExp(new RegExp('another regexp'));
Nov 16, 2016
Nov 16, 2016
894
// Returns: true
May 29, 2016
May 29, 2016
895
util.isRegExp({});
Nov 16, 2016
Nov 16, 2016
896
// Returns: false
Jan 21, 2016
Jan 21, 2016
897
```
Jan 14, 2015
Jan 14, 2015
898
May 23, 2016
May 23, 2016
899
### util.isString(object)
Aug 23, 2016
Aug 23, 2016
900
<!-- YAML
901
added: v0.11.5
902
deprecated: v4.0.0
903
-->
May 21, 2013
May 21, 2013
904
Aug 4, 2016
Aug 4, 2016
905
> Stability: 0 - Deprecated
May 7, 2014
May 7, 2014
906
May 23, 2016
May 23, 2016
907
* `object` {any}
908
909
Returns `true` if the given `object` is a `string`. Otherwise, returns `false`.
May 7, 2014
May 7, 2014
910
Jan 21, 2016
Jan 21, 2016
911
```js
912
const util = require('util');
913
May 29, 2016
May 29, 2016
914
util.isString('');
Nov 16, 2016
Nov 16, 2016
915
// Returns: true
May 29, 2016
May 29, 2016
916
util.isString('foo');
Nov 16, 2016
Nov 16, 2016
917
// Returns: true
May 29, 2016
May 29, 2016
918
util.isString(String('foo'));
Nov 16, 2016
Nov 16, 2016
919
// Returns: true
May 29, 2016
May 29, 2016
920
util.isString(5);
Nov 16, 2016
Nov 16, 2016
921
// Returns: false
Jan 21, 2016
Jan 21, 2016
922
```
Dec 30, 2014
Dec 30, 2014
923
Jun 6, 2016
Jun 6, 2016
924
### util.isSymbol(object)
Aug 23, 2016
Aug 23, 2016
925
<!-- YAML
926
added: v0.11.5
927
deprecated: v4.0.0
928
-->
Dec 30, 2014
Dec 30, 2014
929
Aug 4, 2016
Aug 4, 2016
930
> Stability: 0 - Deprecated
Dec 30, 2014
Dec 30, 2014
931
May 23, 2016
May 23, 2016
932
* `object` {any}
933
934
Returns `true` if the given `object` is a `Symbol`. Otherwise, returns `false`.
Dec 30, 2014
Dec 30, 2014
935
Jan 21, 2016
Jan 21, 2016
936
```js
937
const util = require('util');
May 7, 2014
May 7, 2014
938
May 29, 2016
May 29, 2016
939
util.isSymbol(5);
Nov 16, 2016
Nov 16, 2016
940
// Returns: false
May 29, 2016
May 29, 2016
941
util.isSymbol('foo');
Nov 16, 2016
Nov 16, 2016
942
// Returns: false
May 29, 2016
May 29, 2016
943
util.isSymbol(Symbol('foo'));
Nov 16, 2016
Nov 16, 2016
944
// Returns: true
Jan 21, 2016
Jan 21, 2016
945
```
May 21, 2013
May 21, 2013
946
May 23, 2016
May 23, 2016
947
### util.isUndefined(object)
Aug 23, 2016
Aug 23, 2016
948
<!-- YAML
949
added: v0.11.5
950
deprecated: v4.0.0
951
-->
May 21, 2013
May 21, 2013
952
Aug 4, 2016
Aug 4, 2016
953
> Stability: 0 - Deprecated
May 21, 2013
May 21, 2013
954
May 23, 2016
May 23, 2016
955
* `object` {any}
956
957
Returns `true` if the given `object` is `undefined`. Otherwise, returns `false`.
May 21, 2013
May 21, 2013
958
Jan 21, 2016
Jan 21, 2016
959
```js
960
const util = require('util');
May 21, 2013
May 21, 2013
961
May 29, 2016
May 29, 2016
962
const foo = undefined;
963
util.isUndefined(5);
Nov 16, 2016
Nov 16, 2016
964
// Returns: false
May 29, 2016
May 29, 2016
965
util.isUndefined(foo);
Nov 16, 2016
Nov 16, 2016
966
// Returns: true
May 29, 2016
May 29, 2016
967
util.isUndefined(null);
Nov 16, 2016
Nov 16, 2016
968
// Returns: false
Jan 21, 2016
Jan 21, 2016
969
```
May 21, 2013
May 21, 2013
970
May 23, 2016
May 23, 2016
971
### util.log(string)
Aug 23, 2016
Aug 23, 2016
972
<!-- YAML
973
added: v0.3.0
974
deprecated: v6.0.0
975
-->
May 21, 2013
May 21, 2013
976
Aug 4, 2016
Aug 4, 2016
977
> Stability: 0 - Deprecated: Use a third party module instead.
Apr 25, 2016
Apr 25, 2016
978
Mar 2, 2017
Mar 2, 2017
979
* `string` {string}
May 23, 2016
May 23, 2016
980
981
The `util.log()` method prints the given `string` to `stdout` with an included
982
timestamp.
May 21, 2013
May 21, 2013
983
May 23, 2016
May 23, 2016
984
```js
May 29, 2016
May 29, 2016
985
const util = require('util');
May 23, 2016
May 23, 2016
986
987
util.log('Timestamped message.');
988
```
May 21, 2013
May 21, 2013
989
Sep 14, 2016
Sep 14, 2016
990
### util.print([...strings])
Aug 23, 2016
Aug 23, 2016
991
<!-- YAML
992
added: v0.3.0
993
deprecated: v0.11.3
994
-->
May 21, 2013
May 21, 2013
995
Aug 4, 2016
Aug 4, 2016
996
> Stability: 0 - Deprecated: Use [`console.log()`][] instead.
May 21, 2013
May 21, 2013
997
998
Deprecated predecessor of `console.log`.
999
Sep 14, 2016
Sep 14, 2016
1000
### util.puts([...strings])