diff options
author | Guy Bedford <guybedford@gmail.com> | 2020-09-09 08:27:55 +0300 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2020-09-11 15:09:44 +0300 |
commit | 3fb7fcd821a9aea27a19ef5f7ffcf5120acb859a (patch) | |
tree | dcea6032dab9db225e222b382e32e74f0e1c5462 /lib/internal/modules | |
parent | 02e925553d079c1e70b48e5255503d8850eb1fc5 (diff) |
esm: better package.json parser errors
PR-URL: https://github.com/nodejs/node/pull/35117
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib/internal/modules')
-rw-r--r-- | lib/internal/modules/esm/resolve.js | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 0a828867692..7091a55a5c4 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -82,7 +82,7 @@ function tryStatSync(path) { } } -function getPackageConfig(path) { +function getPackageConfig(path, specifier, base) { const existing = packageJSONCache.get(path); if (existing !== undefined) { return existing; @@ -106,7 +106,11 @@ function getPackageConfig(path) { try { packageJSON = JSONParse(source); } catch (error) { - throw new ERR_INVALID_PACKAGE_CONFIG(path, null, error.message); + throw new ERR_INVALID_PACKAGE_CONFIG( + path, + (base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier), + error.message + ); } let { imports, main, name, type } = packageJSON; @@ -130,13 +134,14 @@ function getPackageConfig(path) { return packageConfig; } -function getPackageScopeConfig(resolved, base) { +function getPackageScopeConfig(resolved) { let packageJSONUrl = new URL('./package.json', resolved); while (true) { const packageJSONPath = packageJSONUrl.pathname; if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json')) break; - const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl), base); + const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl), + resolved); if (packageConfig.exists) return packageConfig; const lastPackageJSONUrl = packageJSONUrl; @@ -497,7 +502,7 @@ function packageImportsResolve(name, base, conditions) { throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base)); } let packageJSONUrl; - const packageConfig = getPackageScopeConfig(base, base); + const packageConfig = getPackageScopeConfig(base); if (packageConfig.exists) { packageJSONUrl = pathToFileURL(packageConfig.pjsonPath); const imports = packageConfig.imports; @@ -535,7 +540,7 @@ function packageImportsResolve(name, base, conditions) { } function getPackageType(url) { - const packageConfig = getPackageScopeConfig(url, url); + const packageConfig = getPackageScopeConfig(url); return packageConfig.type; } @@ -580,7 +585,7 @@ function packageResolve(specifier, base, conditions) { StringPrototypeSlice(specifier, separatorIndex)); // ResolveSelf - const packageConfig = getPackageScopeConfig(base, base); + const packageConfig = getPackageScopeConfig(base); if (packageConfig.exists) { const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath); if (packageConfig.name === packageName && @@ -608,7 +613,7 @@ function packageResolve(specifier, base, conditions) { } // Package match. - const packageConfig = getPackageConfig(packageJSONPath, base); + const packageConfig = getPackageConfig(packageJSONPath, specifier, base); if (packageConfig.exports !== undefined && packageConfig.exports !== null) return packageExportsResolve( packageJSONUrl, packageSubpath, packageConfig, base, conditions |