diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index ed0f201bdb2de7..5327c486afd58f 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -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; @@ -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: diff --git a/test/es-module/test-esm-exports.mjs b/test/es-module/test-esm-exports.mjs index bdd4a975cf748e..9aad720db8cb6f 100644 --- a/test/es-module/test-esm-exports.mjs +++ b/test/es-module/test-esm-exports.mjs @@ -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) { @@ -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([ @@ -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`); })); }