diff --git a/lib/internal/modules/package_map.js b/lib/internal/modules/package_map.js index 0bee02a20789a1..38e7e12206c9ce 100644 --- a/lib/internal/modules/package_map.js +++ b/lib/internal/modules/package_map.js @@ -140,6 +140,8 @@ class PackageMap { * @returns {string|null} */ #getKeyForPath(filePath) { + filePath = pathResolve(filePath); + const cached = this.#pathToKeyCache.get(filePath); if (cached !== undefined) { return cached; } diff --git a/test/parallel/test-require-package-map.js b/test/parallel/test-require-package-map.js index b65a8f4db5a7da..87786d62738a41 100644 --- a/test/parallel/test-require-package-map.js +++ b/test/parallel/test-require-package-map.js @@ -300,6 +300,22 @@ describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PAR describe('longest path wins', () => { const longestPathMap = fixtures.path('package-map/package-map-longest-path.json'); + it('resolves from createRequire() paths with mixed separators', () => { + const { status, stdout, stderr } = spawnSync(process.execPath, [ + '--no-warnings', + '--experimental-package-map', longestPathMap, + '-e', + `const { createRequire } = require('node:module'); const req = createRequire(process.cwd() + '/node_modules/inner/index.js'); const dep = req('dep-a'); console.log(dep.default);`, + ], { + cwd: fixtures.path('package-map/root'), + encoding: 'utf8', + }); + + assert.strictEqual(stderr, ''); + assert.match(stdout, /dep-a-value/); + assert.strictEqual(status, 0, stderr); + }); + it('resolves nested package using its own dependencies, not the parent', () => { // Inner lives at ./root/node_modules/inner which is inside root's // path (./root). The longest matching path should win, so code in