From 2fc8c0f81c3f26093e1678bb7d060549d3b5057d Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Tue, 19 Sep 2023 22:16:55 -0500 Subject: [PATCH] fix: detect valid --- lib/internal/modules/esm/resolve.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index c3ae5e56e333cc..5312cc8026a156 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -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}` : ''; @@ -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);