Skip to content

Commit 05076ac

Browse files
joyeecheungMylesBorins
authored andcommitted
errors: lazy load util in internal/errors.js
Backport-PR-URL: #19191 PR-URL: #18358 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me>
1 parent 4a55524 commit 05076ac

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib/internal/errors.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ const messages = new Map();
1414
const { defineProperty } = Object;
1515

1616
// Lazily loaded
17-
var util = null;
17+
var util_ = null;
18+
function lazyUtil() {
19+
if (!util_) {
20+
util_ = require('util');
21+
}
22+
return util_;
23+
}
1824

1925
function makeNodeError(Base) {
2026
return class NodeError extends Base {
@@ -65,11 +71,11 @@ class AssertionError extends Error {
6571
if (message) {
6672
super(message);
6773
} else {
74+
const util = lazyUtil();
6875
if (actual && actual.stack && actual instanceof Error)
6976
actual = `${actual.name}: ${actual.message}`;
7077
if (expected && expected.stack && expected instanceof Error)
7178
expected = `${expected.name}: ${expected.message}`;
72-
if (util === null) util = require('util');
7379
super(`${util.inspect(actual).slice(0, 128)} ` +
7480
`${operator} ${util.inspect(expected).slice(0, 128)}`);
7581
}
@@ -104,7 +110,7 @@ function message(key, args) {
104110
if (typeof msg === 'function') {
105111
fmt = msg;
106112
} else {
107-
if (util === null) util = require('util');
113+
const util = lazyUtil();
108114
fmt = util.format;
109115
if (args === undefined || args.length === 0)
110116
return msg;

0 commit comments

Comments
 (0)