From 05076ac87b66d92cac46ffaee7874801ead413fb Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 25 Jan 2018 04:39:02 +0800 Subject: [PATCH] errors: lazy load util in internal/errors.js Backport-PR-URL: https://github.com/nodejs/node/pull/19191 PR-URL: https://github.com/nodejs/node/pull/18358 Reviewed-By: James M Snell Reviewed-By: Jon Moss --- lib/internal/errors.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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;