Skip to content

Commit

Permalink
fixup! module: clarify CJS global-like variables not defined error me…
Browse files Browse the repository at this point in the history
…ssage
  • Loading branch information
aduh95 committed Mar 25, 2021
1 parent 8798fb3 commit 46bdb40
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
15 changes: 7 additions & 8 deletions lib/internal/modules/esm/module_job.js
Expand Up @@ -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;
Expand Down
18 changes: 1 addition & 17 deletions lib/internal/modules/esm/resolve.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -909,7 +893,7 @@ module.exports = {
DEFAULT_CONDITIONS,
defaultResolve,
encodedSepRegEx,
getCachedPackageScopeURL,
getPackageScopeConfig,
getPackageType,
packageExportsResolve,
packageImportsResolve
Expand Down

0 comments on commit 46bdb40

Please sign in to comment.