Skip to content

Commit

Permalink
fix: detect valid
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Sep 20, 2023
1 parent 57dc1e2 commit 2fc8c0f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,6 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) {

const baseStringified = isURL(base) ? base.href : base;

if (typeof packageConfig.main !== 'string') {
throw new ERR_INVALID_PACKAGE_CONFIG(
fileURLToPath(packageJSONUrl), base,
'"main" must be a string', 'main');
}

const resolvedOption = FSLegacyMainResolve(packageJsonUrlString, packageConfig.main, baseStringified);

const baseUrl = resolvedOption <= legacyMainResolveExtensionsIndexes.kResolvedByMainIndexNode ? `./${packageConfig.main}` : '';
Expand Down Expand Up @@ -769,6 +763,13 @@ function packageResolve(specifier, base, conditions) {

// Package match.
const packageConfig = packageJsonReader.read(packageJSONPath, { __proto__: null, specifier, base, isESM: true });
const invalid = packageConfig.main === undefined && packageConfig.exports === undefined;
if (invalid) {
throw new ERR_INVALID_PACKAGE_CONFIG(
fileURLToPath(packageJSONUrl), base,
'package.json must have a "main" or "exports" field'
);
}
if (packageConfig.exports != null) {
return packageExportsResolve(
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
Expand Down

0 comments on commit 2fc8c0f

Please sign in to comment.