Skip to content

Commit afafad2

Browse files
committed
fixup! fixup! fixup! esm: fix hint on invalid module specifier
1 parent 1731f5d commit afafad2

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lib/internal/modules/esm/resolve.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,11 +942,12 @@ function resolveAsCommonJS(specifier, parentURL) {
942942
// module
943943
const i = StringPrototypeIndexOf(specifier, '/');
944944
const pkg = i === -1 ? specifier : StringPrototypeSlice(specifier, 0, i);
945-
const index = StringPrototypeIndexOf(found, `node_modules${sep}${pkg}${sep}`);
945+
const needle = `node_modules${sep}${pkg}${sep}`;
946+
const index = StringPrototypeIndexOf(found, needle);
946947
if (index !== -1) {
947-
found = ArrayPrototypeJoin(
948+
found = pkg + '/' + ArrayPrototypeJoin(
948949
ArrayPrototypeMap(
949-
StringPrototypeSplit(StringPrototypeSlice(found, index + `node_modules${sep}`.length), sep),
950+
StringPrototypeSplit(StringPrototypeSlice(found, index + needle.length), sep),
950951
// Escape URL-special characters to avoid generating a incorrect suggestion
951952
encodeURIComponent,
952953
),

test/es-module/test-esm-module-not-found-commonjs-hint.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ describe('ESM: module not found hint', { concurrency: true }, () => {
3333
input: 'import obj from "some_module/folder%25with percentage?/index.js"',
3434
expected: / "some_module\/folder%2525with%20percentage%3F\/index\.js"\?/,
3535
},
36+
{
37+
input: 'import "@nodejsscope/pkg/index"',
38+
expected: / "@nodejsscope\/pkg\/index\.js"\?/,
39+
},
3640
{
3741
input: 'import obj from "lone_file.js"',
3842
expected: /node_modules\/lone_file\.js"\?/,

0 commit comments

Comments
 (0)