Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/internal/modules/package_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-require-package-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading