Node.js util 模块

继承、类型判断、调试输出、以及 promisify 之外的那些工具。

Intermediate
Key Points`util.inherits` 是 ES5 时代的遗产,新代码用 `class extends`。

继承

JavaScript
const util = require('node:util');
const { EventEmitter } = require('node:events');

// 老写法(ES5)
function Old() {
  EventEmitter.call(this);
}
util.inherits(Old, EventEmitter);

// 新写法(ES6+,推荐)
class New extends EventEmitter {}

const n = new New();
n.on('x', (v) => console.log('收到', v));
n.emit('x', 1);
Output
收到 1

类型判断

JavaScript
const { types } = require('node:util');

const checks = [
  ['isPromise', Promise.resolve()],
  ['isDate', new Date()],
  ['isRegExp', /x/],
  ['isMap', new Map()],
  ['isTypedArray', new Uint8Array(4)],
  ['isProxy', new Proxy({}, {})],
];

for (const [fn, val] of checks) {
  console.log(fn, types[fn](val));
}
Output
isPromise true
isDate true
isRegExp true
isMap true
isTypedArray true
isProxy true

util.types reads V8 internal type tags, so it beats instanceof and typeof — especially across vm contexts and worker_threads.

调试输出

JavaScript
const util = require('node:util');
const debug = util.debuglog('db');

debug('连接 %s', 'mysql://localhost');   // 默认不输出
Bash
NODE_DEBUG=db node app.js
Output
DB 24188: 连接 mysql://localhost

debuglog is a zero-dependency conditional logger: silent by default, enabled by an env var. Cleaner than sprinkling if (DEBUG).

废弃警告

JavaScript
const util = require('node:util');

const oldApi = util.deprecate(
  (x) => x * 2,
  'oldApi 将在 v3 移除,请改用 newApi',
  'DEP0001'
);

oldApi(2);   // 正常返回值,但打印一条 DeprecationWarning
Output
(node:24188) DeprecationWarning: oldApi 将在 v3 移除,请改用 newApi
(Use `node --trace-deprecation ...` to show where the warning was created)

其他常用

APIUse
util.formatprintf 风格格式化
util.inspect把对象转成可读字符串
util.isDeepStrictEqual严格深比较
util.parseArgs解析命令行参数
util.aborted把 signal 转成 Promise
util.stripVTControlCharacters去掉终端颜色码
JavaScript
const util = require('node:util');

const ac = new AbortController();
setTimeout(() => ac.abort(), 100);

await util.aborted(ac.signal);
console.log('已中止');
Output
已中止

Friends & Partners