Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,6 @@ function applyExports(basePath, expansion) {
basePath, mappingKey);
}

// Fallback to CJS main lookup when no main export is defined
if (mappingKey === '.')
return basePath;

let dirMatch = '';
for (const candidateKey of ObjectKeys(pkgExports)) {
if (candidateKey[candidateKey.length - 1] !== '/') continue;
Expand All @@ -524,15 +520,8 @@ function applyExports(basePath, expansion) {
subpath, basePath, mappingKey);
}
}
// Fallback to CJS main lookup when no main export is defined
if (mappingKey === '.')
return basePath;

// eslint-disable-next-line no-restricted-syntax
const e = new Error(`Package exports for '${basePath}' do not define ` +
`a '${mappingKey}' subpath`);
e.code = 'MODULE_NOT_FOUND';
throw e;
// Fallback to normal CJS lookup when no export is defined
return basePath + expansion;
}

// This only applies to requests of a specific form:
Expand Down
24 changes: 17 additions & 7 deletions test/es-module/test-esm-exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/resolve-self', isRequire ?
{ default: 'self-cjs' } : { default: 'self-mjs' }],
// Resolve self sugar
['pkgexports-sugar', { default: 'main' }]
['pkgexports-sugar', { default: 'main' }],
...!isRequire ? [] : [
// Sugar cases still encapsulate
['pkgexports-sugar/not-exported.js', { default: 'not-exported' }],
['pkgexports-sugar2/not-exported.js', { default: 'not-exported' }],
['pkgexports-number/hidden.js', { default: 'not-part-of-api' }]
],
]);

for (const [validSpecifier, expected] of validSpecifiers) {
Expand All @@ -49,10 +55,12 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/missing', './missing'],
// The file exists but isn't exported. The exports is a number which counts
// as a non-null value without any properties, just like `{}`.
['pkgexports-number/hidden.js', './hidden.js'],
// Sugar cases still encapsulate
['pkgexports-sugar/not-exported.js', './not-exported.js'],
['pkgexports-sugar2/not-exported.js', './not-exported.js'],
...isRequire ? [] : [
// Sugar cases still encapsulate
['pkgexports-sugar/not-exported.js', './not-exported.js'],
['pkgexports-sugar2/not-exported.js', './not-exported.js'],
['pkgexports-number/hidden.js', './hidden.js']
],
]);

const invalidExports = new Map([
Expand Down Expand Up @@ -82,8 +90,10 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
for (const [specifier, subpath] of undefinedExports) {
loadFixture(specifier).catch(mustCall((err) => {
strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
assertStartsWith(err.message, 'Package exports');
assertIncludes(err.message, `do not define a '${subpath}' subpath`);
assertStartsWith(err.message, isRequire ? 'Cannot find module' :
'Package exports');
if (!isRequire)
assertIncludes(err.message, `do not define a '${subpath}' subpath`);
}));
}

Expand Down