Skip to content

Commit

Permalink
esm: fix misleading when import empty package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Sep 20, 2023
1 parent e9ff810 commit 57dc1e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ 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
23 changes: 23 additions & 0 deletions test/es-module/test-import-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const { spawnPromisified } = require('../common');
const fixtures = require('../common/fixtures.js');
const assert = require('node:assert');
const { execPath } = require('node:process');
const { describe, it } = require('node:test');

describe('Import empty module', { concurrency: true }, () => {
it(async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--eval',
'import("empty")',
], {
cwd: fixtures.path(),
});
assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
assert.strictEqual(stdout, '');
assert.match(stderr, /Error \[ERR_INVALID_PACKAGE_CONFIG]: Invalid package config/);
});
});
1 change: 1 addition & 0 deletions test/fixtures/node_modules/empty/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 57dc1e2

Please sign in to comment.