Skip to content

Commit e540d5c

Browse files
MylesBorinsBridgeAR
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 bfa19c4 commit e540d5c

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,
@@ -1100,18 +1101,28 @@ E('ERR_INVALID_PACKAGE_CONFIG', (path, message, hasMessage = true) => {
11001101
}, Error);
11011102
E('ERR_INVALID_PACKAGE_TARGET',
11021103
(pkgPath, key, subpath, target, base = undefined) => {
1104+
const relError = typeof target === 'string' &&
1105+
target.length && !StringPrototypeStartsWith(target, './');
11031106
if (key === null) {
11041107
if (subpath !== '') {
11051108
return `Invalid "exports" target ${JSONStringify(target)} defined ` +
11061109
`for '${subpath}' in the package config ${pkgPath} imported from ` +
1107-
base;
1110+
`${base}.${relError ? '; targets must start with "./"' : ''}`;
11081111
} else {
11091112
return `Invalid "exports" main target ${target} defined in the ` +
1110-
`package config ${pkgPath} imported from ${base}.`;
1113+
`package config ${pkgPath} imported from ${base}${relError ?
1114+
'; targets must start with "./"' : ''}`;
11111115
}
11121116
} else if (key === '.') {
11131117
return `Invalid "exports" main target ${JSONStringify(target)} defined ` +
1114-
`in the package config ${pkgPath}${sep}package.json`;
1118+
`in the package config ${pkgPath}${sep}package.json${relError ?
1119+
'; targets must start with "./"' : ''}`;
1120+
} else if (typeof target === 'string' && target !== '' &&
1121+
!StringPrototypeStartsWith(target, './')) {
1122+
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
1123+
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
1124+
`package config ${pkgPath}${sep}package.json; ` +
1125+
'targets must start with "./"';
11151126
} else {
11161127
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
11171128
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)