Skip to content

Commit 40ab9c3

Browse files
committed
errors: faster ERR_MODULE_NOT_FOUND error
The function calls are more expensive than using the error printf format style. Signed-off-by: Ruben Bridgewater <[email protected]>
1 parent ee8e40f commit 40ab9c3

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

lib/internal/errors.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,9 +1383,7 @@ E('ERR_MISSING_ARGS',
13831383
return `${msg} must be specified`;
13841384
}, TypeError);
13851385
E('ERR_MISSING_OPTION', '%s is required', TypeError);
1386-
E('ERR_MODULE_NOT_FOUND', (path, base, type = 'package') => {
1387-
return `Cannot find ${type} '${path}' imported from ${base}`;
1388-
}, Error);
1386+
E('ERR_MODULE_NOT_FOUND', "Cannot find %s '%s' imported from %s", Error);
13891387
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error);
13901388
E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function', TypeError);
13911389
E('ERR_NAPI_INVALID_DATAVIEW_ARGS',

lib/internal/modules/esm/fetch_module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function fetchWithRedirects(parsed) {
144144
return entry;
145145
}
146146
if (res.statusCode === 404) {
147-
const err = new ERR_MODULE_NOT_FOUND(parsed.href, null);
147+
const err = new ERR_MODULE_NOT_FOUND('package', parsed.href, null);
148148
err.message = `Cannot find module '${parsed.href}', HTTP 404`;
149149
throw err;
150150
}

lib/internal/modules/esm/resolve.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) {
200200
}
201201
// Not found.
202202
throw new ERR_MODULE_NOT_FOUND(
203-
fileURLToPath(new URL('.', packageJSONUrl)), fileURLToPath(base));
203+
'package',
204+
fileURLToPath(new URL('.', packageJSONUrl)),
205+
fileURLToPath(base),
206+
);
204207
}
205208

206209
const encodedSepRegEx = /%2F|%5C/i;
@@ -229,7 +232,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
229232
process.send({ 'watch:require': [path || resolved.pathname] });
230233
}
231234
throw new ERR_MODULE_NOT_FOUND(
232-
path || resolved.pathname, base && fileURLToPath(base), 'module');
235+
'module', path || resolved.pathname, base && fileURLToPath(base));
233236
}
234237

235238
if (!preserveSymlinks) {
@@ -791,7 +794,7 @@ function packageResolve(specifier, base, conditions) {
791794

792795
// eslint can't handle the above code.
793796
// eslint-disable-next-line no-unreachable
794-
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base));
797+
throw new ERR_MODULE_NOT_FOUND('package', packageName, fileURLToPath(base));
795798
}
796799

797800
/**

0 commit comments

Comments
 (0)