Skip to content

Commit 8946387

Browse files
committed
fix(esm): misleading when import empty package.json
1 parent e9ff810 commit 8946387

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

lib/internal/modules/esm/resolve.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) {
183183

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

186+
if (typeof packageConfig.main !== 'string') {
187+
throw new ERR_INVALID_PACKAGE_CONFIG(
188+
fileURLToPath(packageJSONUrl), base,
189+
'"main" must be a string', 'main');
190+
}
191+
186192
const resolvedOption = FSLegacyMainResolve(packageJsonUrlString, packageConfig.main, baseStringified);
187193

188194
const baseUrl = resolvedOption <= legacyMainResolveExtensionsIndexes.kResolvedByMainIndexNode ? `./${packageConfig.main}` : '';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const { spawnPromisified } = require('../common');
4+
const fixtures = require('../common/fixtures.js');
5+
const assert = require('node:assert');
6+
const { execPath } = require('node:process');
7+
const { describe, it } = require('node:test');
8+
9+
describe('Import empty module', { concurrency: true }, () => {
10+
it(async () => {
11+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
12+
'--no-warnings',
13+
'--eval',
14+
'import("empty")',
15+
], {
16+
cwd: fixtures.path(),
17+
});
18+
assert.strictEqual(code, 1);
19+
assert.strictEqual(signal, null);
20+
assert.strictEqual(stdout, '');
21+
assert.match(stderr, /Error \[ERR_INVALID_PACKAGE_CONFIG]: Invalid package config/);
22+
});
23+
});

test/fixtures/node_modules/empty/package.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)