diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 09c0265c8be2a2..61c043e35c6ce9 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -227,9 +227,15 @@ const encodedSepRegEx = /%2F|%5C/i; */ function finalizeResolution(resolved, base, preserveSymlinks) { if (RegExpPrototypeExec(encodedSepRegEx, resolved.pathname) !== null) { + let basePath; + try { + basePath = fileURLToPath(base); + } catch { + basePath = base; + } throw new ERR_INVALID_MODULE_SPECIFIER( resolved.pathname, 'must not include encoded "/" or "\\" characters', - fileURLToPath(base)); + basePath); } let path; @@ -248,14 +254,26 @@ function finalizeResolution(resolved, base, preserveSymlinks) { // Check for stats.isDirectory() if (stats === 1) { - throw new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base), String(resolved)); + let basePath; + try { + basePath = fileURLToPath(base); + } catch { + basePath = base; + } + throw new ERR_UNSUPPORTED_DIR_IMPORT(path, basePath, String(resolved)); } else if (stats !== 0) { // Check for !stats.isFile() if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) { process.send({ 'watch:require': [path || resolved.pathname] }); } + let basePath; + try { + basePath = fileURLToPath(base); + } catch { + basePath = base; + } throw new ERR_MODULE_NOT_FOUND( - path || resolved.pathname, base && fileURLToPath(base), resolved); + path || resolved.pathname, basePath, resolved); } if (!preserveSymlinks) { diff --git a/test/es-module/test-esm-main-lookup.mjs b/test/es-module/test-esm-main-lookup.mjs index 4f4f1c378914d7..30042514f26e0e 100644 --- a/test/es-module/test-esm-main-lookup.mjs +++ b/test/es-module/test-esm-main-lookup.mjs @@ -15,6 +15,14 @@ await assert.rejects(import('../fixtures/es-modules/pjson-main'), { code: 'ERR_UNSUPPORTED_DIR_IMPORT', url: fixtures.fileURL('es-modules/pjson-main').href, }); +await assert.rejects(import(`data:text/javascript,import${encodeURIComponent(JSON.stringify(fixtures.fileURL('es-modules/pjson-main')))}`), { + code: 'ERR_UNSUPPORTED_DIR_IMPORT', + url: fixtures.fileURL('es-modules/pjson-main').href, +}); +await assert.rejects(import(`data:text/javascript,import${encodeURIComponent(JSON.stringify(fixtures.fileURL('es-modules/does-not-exist')))}`), { + code: 'ERR_MODULE_NOT_FOUND', + url: fixtures.fileURL('es-modules/does-not-exist').href, +}); assert.deepStrictEqual( { ...await import('../fixtures/es-modules/pjson-main/main.mjs') },