Skip to content

Commit 974e6c0

Browse files
committed
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
1 parent 22b6997 commit 974e6c0

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

lib/internal/errors.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1104,18 +1104,28 @@ E('ERR_INVALID_PACKAGE_CONFIG', (path, message, hasMessage = true) => {
11041104
}, Error);
11051105
E('ERR_INVALID_PACKAGE_TARGET',
11061106
(pkgPath, key, subpath, target, base = undefined) => {
1107+
const relError = typeof target === 'string'
1108+
&& target.length && !target.startsWith('./');
11071109
if (key === null) {
11081110
if (subpath !== '') {
11091111
return `Invalid "exports" target ${JSONStringify(target)} defined ` +
11101112
`for '${subpath}' in the package config ${pkgPath} imported from ` +
1111-
base;
1113+
`${base}.${relError ? ' - targets must start with "./"' : ''}`;
11121114
} else {
11131115
return `Invalid "exports" main target ${target} defined in the ` +
1114-
`package config ${pkgPath} imported from ${base}.`;
1116+
`package config ${pkgPath} imported from ${base}.${relError ?
1117+
' - targets must start with "./"' : ''}`;
11151118
}
11161119
} else if (key === '.') {
11171120
return `Invalid "exports" main target ${JSONStringify(target)} defined ` +
1118-
`in the package config ${pkgPath}${sep}package.json`;
1121+
`in the package config ${pkgPath}${sep}package.json${relError ?
1122+
' - targets must start with "./"' : ''}`;
1123+
} else if (typeof target === 'string' && target !== '' &&
1124+
!target.startsWith('./')) {
1125+
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
1126+
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
1127+
`package config ${pkgPath}${sep}package.json. ` +
1128+
'- targets must start with `./`';
11191129
} else {
11201130
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
11211131
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)