diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 9f3131b7dfc136..bf4788d1855881 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -178,18 +178,17 @@ class ModuleJob { e.message += ', you can use import instead'; } - const packageJSONUrl = + const packageConfig = StringPrototypeStartsWith(this.module.url, 'file://') && RegExpPrototypeTest(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) && require('internal/modules/esm/resolve') - .getCachedPackageScopeURL(this.module.url); - if (packageJSONUrl) { + .getPackageScopeConfig(this.module.url); + if (packageConfig.type === 'module') { e.message += - `\n'${this.module.url}' cannot be parsed as an ES module, but ` + - 'it is being treated as an ES module because it has a \'.js\' ' + - `file extension and '${packageJSONUrl}' contains "type": ` + - '"module". To treat it as a CommonJS script, rename it to use ' + - 'the \'.cjs\' file extension.'; + '\nThis file is being treated as an ES module because it has a ' + + `'.js' file extension and '${packageConfig.pjsonPath}' contains ` + + '"type": "module". To treat it as a CommonJS script, rename it ' + + 'to use the \'.cjs\' file extension.'; } } throw e; diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 720fc536d1a75b..7b503376af0950 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -173,22 +173,6 @@ function getPackageConfig(path, specifier, base) { return packageConfig; } -function getCachedPackageScopeURL(url) { - let packageJSONUrl = new URL('./package.json', url); - while (true) { - const path = fileURLToPath(packageJSONUrl); - if (packageJSONCache.get(path)?.exists) { - return packageJSONUrl; - } - const previousPathname = packageJSONUrl.pathname; - packageJSONUrl = new URL('../package.json', packageJSONUrl); - - // Terminates at root where ../package.json equals ../../package.json - // (can't just check "/package.json" for Windows support). - if (previousPathname === packageJSONUrl.pathname) return; - } -} - function getPackageScopeConfig(resolved) { let packageJSONUrl = new URL('./package.json', resolved); while (true) { @@ -909,7 +893,7 @@ module.exports = { DEFAULT_CONDITIONS, defaultResolve, encodedSepRegEx, - getCachedPackageScopeURL, + getPackageScopeConfig, getPackageType, packageExportsResolve, packageImportsResolve