module: avoid reparsing package.json in esm format detection

Determining the format of a `.js` or `.ts` module called
`getPackageScopeConfig()`, which spread the deserialized package
configuration and thereby invoked its lazy `exports` and `imports`
getters. Every resolved module parsed those maps again, so load time
and memory grew with the number of modules in a package times the
size of its `exports` map.

Use `getPackageType()` for format detection and only look up the
package.json path when warning about a typeless package. Stop
spreading the configuration in `getPackageScopeConfig()` so that the
maps are parsed only when accessed.

Fixes: https://github.com/nodejs/node/issues/66485
Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
PR-URL: https://github.com/nodejs/node/pull/66982
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Gürgün Dayıoğlu authored and GitHub committed 2026-11-02 14:51:36 +00:00
1 parent 1389475f66
commit 45df6b73d5
2 files changed
+21 -18

No files matched your search

+16 -12
View File
@@ -149,9 +149,17 @@ function underNodeModules(url) {
}
let typelessPackageJsonFilesWarnedAbout;
function warnTypelessPackageJsonFile(pjsonPath, url) {
/**
* Called for a module with a package.json without a `type` field (or none at all) and ESM syntax.
* Warns about the missing `type` field so that the user can avoid the performance penalty of detection.
* @param {URL} url
*/
function warnIfTypelessPackageJsonFile(url) {
if (underNodeModules(url)) { return; }
const { pjsonPath, exists } = getPackageScopeConfig(url);
if (!exists) { return; }
typelessPackageJsonFilesWarnedAbout ??= new SafeSet();
if (!underNodeModules(url) && !typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) {
if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) {
const warning = `Module type of ${url} is not specified and it doesn't parse as CommonJS.\n` +
'Reparsing as ES module because module syntax was detected. This incurs a performance overhead.\n' +
`To eliminate this warning, add "type": "module" to ${pjsonPath}.`;
@@ -173,7 +181,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
const ext = extname(url);
if (ext === '.js') {
const { type: packageType, pjsonPath, exists: foundPackageJson } = getPackageScopeConfig(url);
const packageType = getPackageType(url);
if (packageType !== 'none') {
return packageType;
}
@@ -183,15 +191,13 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
// but this gets called again from `defaultLoad`/`defaultLoadSync`.
// For ambiguous files (.js, no type field) we return undefined from `resolve` and re-run the check in `load`.
const format = detectModuleFormat(source, url);
if (format === 'module' && foundPackageJson) {
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
warnTypelessPackageJsonFile(pjsonPath, url);
if (format === 'module') {
warnIfTypelessPackageJsonFile(url);
}
return format;
}
if (ext === '.ts' && getOptionValue('--strip-types')) {
const { type: packageType, pjsonPath, exists: foundPackageJson } = getPackageScopeConfig(url);
const packageType = getPackageType(url);
if (packageType !== 'none') {
return `${packageType}-typescript`;
}
@@ -206,10 +212,8 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
const parsedSource = stripTypeScriptModuleTypes(stringifiedSource, fileURLToPath(url));
const detectedFormat = detectModuleFormat(parsedSource, url);
const format = `${detectedFormat}-typescript`;
if (format === 'module-typescript' && foundPackageJson) {
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
warnTypelessPackageJsonFile(pjsonPath, url);
if (format === 'module-typescript') {
warnIfTypelessPackageJsonFile(url);
}
return format;
}
+5 -6
View File
@@ -264,12 +264,11 @@ function getPackageScopeConfig(resolved) {
if (ArrayIsArray(result)) {
const { data, exists, path } = deserializePackageJSON(`${resolved}`, result);
return {
__proto__: null,
...data,
exists,
pjsonPath: path,
};
// `data` is not shared, so extend it in place instead of spreading it:
// spreading would invoke the lazy `imports` and `exports` getters.
data.exists = exists;
data.pjsonPath = path;
return data;
}
// This means that the response is a string