Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bf7b796

Browse filesBrowse files
guybedfordcodebytere
authored andcommitted
esm: better package.json parser errors
PR-URL: #35117 Backport-PR-URL: #35385 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 9159649 commit bf7b796
Copy full SHA for bf7b796

File tree

Expand file treeCollapse file tree

4 files changed

+18
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+18
-14
lines changed
Open diff view settings
Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ E('ERR_INVALID_OPT_VALUE', (name, value) =>
11071107
E('ERR_INVALID_OPT_VALUE_ENCODING',
11081108
'The value "%s" is invalid for option "encoding"', TypeError);
11091109
E('ERR_INVALID_PACKAGE_CONFIG', (path, base, message) => {
1110-
return `Invalid package config ${path}${base ? ` imported from ${base}` :
1110+
return `Invalid package config ${path}${base ? ` while importing ${base}` :
11111111
''}${message ? `. ${message}` : ''}`;
11121112
}, Error);
11131113
E('ERR_INVALID_PACKAGE_TARGET',
Collapse file

‎lib/internal/modules/esm/resolve.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+13-8Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function tryStatSync(path) {
7777
}
7878
}
7979

80-
function getPackageConfig(path) {
80+
function getPackageConfig(path, specifier, base) {
8181
const existing = packageJSONCache.get(path);
8282
if (existing !== undefined) {
8383
return existing;
@@ -101,7 +101,11 @@ function getPackageConfig(path) {
101101
try {
102102
packageJSON = JSONParse(source);
103103
} catch (error) {
104-
throw new ERR_INVALID_PACKAGE_CONFIG(path, null, error.message);
104+
throw new ERR_INVALID_PACKAGE_CONFIG(
105+
path,
106+
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier),
107+
error.message
108+
);
105109
}
106110

107111
let { imports, main, name, type } = packageJSON;
@@ -125,13 +129,14 @@ function getPackageConfig(path) {
125129
return packageConfig;
126130
}
127131

128-
function getPackageScopeConfig(resolved, base) {
132+
function getPackageScopeConfig(resolved) {
129133
let packageJSONUrl = new URL('./package.json', resolved);
130134
while (true) {
131135
const packageJSONPath = packageJSONUrl.pathname;
132136
if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json'))
133137
break;
134-
const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl), base);
138+
const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl),
139+
resolved);
135140
if (packageConfig.exists) return packageConfig;
136141

137142
const lastPackageJSONUrl = packageJSONUrl;
@@ -492,7 +497,7 @@ function packageImportsResolve(name, base, conditions) {
492497
throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base));
493498
}
494499
let packageJSONUrl;
495-
const packageConfig = getPackageScopeConfig(base, base);
500+
const packageConfig = getPackageScopeConfig(base);
496501
if (packageConfig.exists) {
497502
packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
498503
const imports = packageConfig.imports;
@@ -530,7 +535,7 @@ function packageImportsResolve(name, base, conditions) {
530535
}
531536

532537
function getPackageType(url) {
533-
const packageConfig = getPackageScopeConfig(url, url);
538+
const packageConfig = getPackageScopeConfig(url);
534539
return packageConfig.type;
535540
}
536541

@@ -575,7 +580,7 @@ function packageResolve(specifier, base, conditions) {
575580
StringPrototypeSlice(specifier, separatorIndex));
576581

577582
// ResolveSelf
578-
const packageConfig = getPackageScopeConfig(base, base);
583+
const packageConfig = getPackageScopeConfig(base);
579584
if (packageConfig.exists) {
580585
const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
581586
if (packageConfig.name === packageName &&
@@ -603,7 +608,7 @@ function packageResolve(specifier, base, conditions) {
603608
}
604609

605610
// Package match.
606-
const packageConfig = getPackageConfig(packageJSONPath, base);
611+
const packageConfig = getPackageConfig(packageJSONPath, specifier, base);
607612
if (packageConfig.exports !== undefined && packageConfig.exports !== null)
608613
return packageExportsResolve(
609614
packageJSONUrl, packageSubpath, packageConfig, base, conditions
Collapse file

‎test/es-module/test-esm-invalid-pjson.js‎

Copy file name to clipboardExpand all lines: test/es-module/test-esm-invalid-pjson.js
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ child.on('close', mustCall((code, signal) => {
1919
strictEqual(signal, null);
2020
ok(
2121
stderr.includes(
22-
[
23-
'[ERR_INVALID_PACKAGE_CONFIG]: ',
24-
`Invalid package config ${invalidJson}. `,
25-
`Unexpected token } in JSON at position ${isWindows ? 16 : 14}`
26-
].join(''),
22+
`[ERR_INVALID_PACKAGE_CONFIG]: Invalid package config ${invalidJson} ` +
23+
`while importing "invalid-pjson" from ${entry}. ` +
24+
`Unexpected token } in JSON at position ${isWindows ? 16 : 14}`
2725
),
2826
stderr);
2927
}));
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
syntax error

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.