Skip to content

Commit 3dc3772

Browse files
MylesBorinstargos
authored andcommitted
module: improve error for invalid package targets
For targets that are strings that do not start with `./` or `/` the error will now have additional information about what the programming error is. Closes: #32034 PR-URL: #32052 Fixes: #32034 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Signed-off-by: Myles Borins <[email protected]>
1 parent becbe9e commit 3dc3772

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lib/internal/errors.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
ObjectDefineProperty,
2121
ObjectKeys,
2222
StringPrototypeSlice,
23+
StringPrototypeStartsWith,
2324
Symbol,
2425
SymbolFor,
2526
WeakMap,
@@ -1096,18 +1097,28 @@ E('ERR_INVALID_PACKAGE_CONFIG', (path, message, hasMessage = true) => {
10961097
}, Error);
10971098
E('ERR_INVALID_PACKAGE_TARGET',
10981099
(pkgPath, key, subpath, target, base = undefined) => {
1100+
const relError = typeof target === 'string' &&
1101+
target.length && !StringPrototypeStartsWith(target, './');
10991102
if (key === null) {
11001103
if (subpath !== '') {
11011104
return `Invalid "exports" target ${JSONStringify(target)} defined ` +
11021105
`for '${subpath}' in the package config ${pkgPath} imported from ` +
1103-
base;
1106+
`${base}.${relError ? '; targets must start with "./"' : ''}`;
11041107
} else {
11051108
return `Invalid "exports" main target ${target} defined in the ` +
1106-
`package config ${pkgPath} imported from ${base}.`;
1109+
`package config ${pkgPath} imported from ${base}${relError ?
1110+
'; targets must start with "./"' : ''}`;
11071111
}
11081112
} else if (key === '.') {
11091113
return `Invalid "exports" main target ${JSONStringify(target)} defined ` +
1110-
`in the package config ${pkgPath}${sep}package.json`;
1114+
`in the package config ${pkgPath}${sep}package.json${relError ?
1115+
'; targets must start with "./"' : ''}`;
1116+
} else if (typeof target === 'string' && target !== '' &&
1117+
!StringPrototypeStartsWith(target, './')) {
1118+
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
1119+
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
1120+
`package config ${pkgPath}${sep}package.json; ` +
1121+
'targets must start with "./"';
11111122
} else {
11121123
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
11131124
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +

test/es-module/test-esm-exports.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
7979
['pkgexports/null', './null'],
8080
['pkgexports/invalid2', './invalid2'],
8181
['pkgexports/invalid3', './invalid3'],
82+
['pkgexports/invalid5', 'invalid5'],
8283
// Missing / invalid fallbacks
8384
['pkgexports/nofallback1', './nofallback1'],
8485
['pkgexports/nofallback2', './nofallback2'],
@@ -107,6 +108,9 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
107108
strictEqual(err.code, 'ERR_INVALID_PACKAGE_TARGET');
108109
assertStartsWith(err.message, 'Invalid "exports"');
109110
assertIncludes(err.message, subpath);
111+
if (!subpath.startsWith('./')) {
112+
assertIncludes(err.message, 'targets must start with');
113+
}
110114
}));
111115
}
112116

test/fixtures/node_modules/pkgexports/package.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)