Skip to content

Commit eaf841d

Browse files
MylesBorinsBethGriggs
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 <webmaster@geoffreybooth.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Signed-off-by: Myles Borins <myles.borins@gmail.com>
1 parent ca5ebcf commit eaf841d

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lib/internal/errors.js

+14-3
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,
@@ -1104,18 +1105,28 @@ E('ERR_INVALID_PACKAGE_CONFIG', (path, message, hasMessage = true) => {
11041105
}, Error);
11051106
E('ERR_INVALID_PACKAGE_TARGET',
11061107
(pkgPath, key, subpath, target, base = undefined) => {
1108+
const relError = typeof target === 'string' &&
1109+
target.length && !StringPrototypeStartsWith(target, './');
11071110
if (key === null) {
11081111
if (subpath !== '') {
11091112
return `Invalid "exports" target ${JSONStringify(target)} defined ` +
11101113
`for '${subpath}' in the package config ${pkgPath} imported from ` +
1111-
base;
1114+
`${base}.${relError ? '; targets must start with "./"' : ''}`;
11121115
} else {
11131116
return `Invalid "exports" main target ${target} defined in the ` +
1114-
`package config ${pkgPath} imported from ${base}.`;
1117+
`package config ${pkgPath} imported from ${base}${relError ?
1118+
'; targets must start with "./"' : ''}`;
11151119
}
11161120
} else if (key === '.') {
11171121
return `Invalid "exports" main target ${JSONStringify(target)} defined ` +
1118-
`in the package config ${pkgPath}${sep}package.json`;
1122+
`in the package config ${pkgPath}${sep}package.json${relError ?
1123+
'; targets must start with "./"' : ''}`;
1124+
} else if (typeof target === 'string' && target !== '' &&
1125+
!StringPrototypeStartsWith(target, './')) {
1126+
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
1127+
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
1128+
`package config ${pkgPath}${sep}package.json; ` +
1129+
'targets must start with "./"';
11191130
} else {
11201131
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
11211132
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +

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

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
7878
['pkgexports/null', './null'],
7979
['pkgexports/invalid2', './invalid2'],
8080
['pkgexports/invalid3', './invalid3'],
81+
['pkgexports/invalid5', 'invalid5'],
8182
// Missing / invalid fallbacks
8283
['pkgexports/nofallback1', './nofallback1'],
8384
['pkgexports/nofallback2', './nofallback2'],
@@ -106,6 +107,9 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
106107
strictEqual(err.code, 'ERR_INVALID_PACKAGE_TARGET');
107108
assertStartsWith(err.message, 'Invalid "exports"');
108109
assertIncludes(err.message, subpath);
110+
if (!subpath.startsWith('./')) {
111+
assertIncludes(err.message, 'targets must start with');
112+
}
109113
}));
110114
}
111115

test/fixtures/node_modules/pkgexports/package.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)