镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 6b07f2f

Browse files
authored
module: fix require of imports with package map
Signed-off-by: lazerg <lazerg2@gmail.com> Assisted-by: Claude Code PR-URL: #66565 Fixes: #66562 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 85543c1 commit 6b07f2f

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
15231523
const conditions = (options?.conditions) || getCjsConditions();
15241524

15251525
// Try package map resolution for bare specifiers (if --experimental-package-map is set)
1526-
if (!isRelative(request) && !path.isAbsolute(request)) {
1526+
if (request[0] !== '#' && !isRelative(request) && !path.isAbsolute(request)) {
15271527
const resolved = tryPackageMapResolveCJS(request, parent, conditions);
15281528
if (resolved !== undefined) {
15291529
return resolved;

‎test/fixtures/package-map/root/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"type": "module",
44
"exports": {
55
".": "./index.js"
6+
},
7+
"imports": {
8+
"#root": "./index.js"
69
}
710
}

‎test/parallel/test-require-package-map.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const common = require('../common');
44
const fixtures = require('../common/fixtures');
5+
const { spawnSyncAndAssert } = require('../common/child_process');
56
const assert = require('node:assert');
67
const { spawnSync } = require('node:child_process');
78
const { symlinkSync, writeFileSync } = require('node:fs');
@@ -132,6 +133,21 @@ describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PAR
132133
assert.strictEqual(status, 0, stderr);
133134
});
134135

136+
it('resolves package imports without consulting the map', () => {
137+
spawnSyncAndAssert(process.execPath, [
138+
'--no-warnings',
139+
'--experimental-package-map', packageMapPath,
140+
'-e',
141+
`const root = require('#root'); console.log(root.default);`,
142+
], {
143+
cwd: fixtures.path('package-map/root'),
144+
encoding: 'utf8',
145+
}, {
146+
stderr: '',
147+
stdout: /root-package/,
148+
});
149+
});
150+
135151
it('throws when parent not in map', () => {
136152
const { status, stderr } = spawnSync(process.execPath, [
137153
'--no-warnings',

0 commit comments

Comments
 (0)