diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 551817b343bf63..1027ef53ebdde6 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -14,7 +14,13 @@ const messages = new Map(); const { defineProperty } = Object; // Lazily loaded -var util = null; +var util_ = null; +function lazyUtil() { + if (!util_) { + util_ = require('util'); + } + return util_; +} function makeNodeError(Base) { return class NodeError extends Base { @@ -65,11 +71,11 @@ class AssertionError extends Error { if (message) { super(message); } else { + const util = lazyUtil(); if (actual && actual.stack && actual instanceof Error) actual = `${actual.name}: ${actual.message}`; if (expected && expected.stack && expected instanceof Error) expected = `${expected.name}: ${expected.message}`; - if (util === null) util = require('util'); super(`${util.inspect(actual).slice(0, 128)} ` + `${operator} ${util.inspect(expected).slice(0, 128)}`); } @@ -104,7 +110,7 @@ function message(key, args) { if (typeof msg === 'function') { fmt = msg; } else { - if (util === null) util = require('util'); + const util = lazyUtil(); fmt = util.format; if (args === undefined || args.length === 0) return msg;