diff --git a/lib/resolve.js b/lib/resolve.js index 505923f..e4e44ac 100644 --- a/lib/resolve.js +++ b/lib/resolve.js @@ -1081,6 +1081,9 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) { export function moduleResolve(specifier, base, conditions, preserveSymlinks) { // Note: The Node code supports `base` as a string (in this internal API) too, // we don’t. + if (conditions === undefined) { + conditions = getConditionsSet() + } const protocol = base.protocol const isData = protocol === 'data:' const isRemote = isData || protocol === 'http:' || protocol === 'https:' diff --git a/test/core.js b/test/core.js index ef78109..aa5416e 100644 --- a/test/core.js +++ b/test/core.js @@ -8,7 +8,7 @@ import process from 'node:process' import {URL, pathToFileURL} from 'node:url' import test from 'node:test' import semver from 'semver' -import {resolve} from '../index.js' +import {moduleResolve, resolve} from '../index.js' const windows = process.platform === 'win32' const nodeBefore18 = semver.lt(process.versions.node, '18.0.0') @@ -258,9 +258,13 @@ test( assert.fail() } catch (error) { const exception = /** @type {ErrnoException} */ (error) - assert.equal( - exception.code, - 'ERR_NETWORK_IMPORT_DISALLOWED', + assert( + [ + // Node 22+ + 'ERR_ASSERTION', + // Node 20- + 'ERR_NETWORK_IMPORT_DISALLOWED' + ].includes(/** @type {string} */ (exception.code)), 'should not support loading builtins from http' ) } @@ -690,3 +694,12 @@ test( ) } ) + +test('moduleResolve(specifier, parent, conditions?, preserveSymlinks?)', function () { + assert.equal( + moduleResolve('package-export-conditions', new URL(import.meta.url)).href, + new URL('node_modules/package-export-conditions/esm.mjs', import.meta.url) + .href, + 'should resolve to the ESM entry point' + ) +}) diff --git a/test/node_modules/package-export-conditions/cjs.js b/test/node_modules/package-export-conditions/cjs.js new file mode 100644 index 0000000..e69de29 diff --git a/test/node_modules/package-export-conditions/esm.mjs b/test/node_modules/package-export-conditions/esm.mjs new file mode 100644 index 0000000..e69de29 diff --git a/test/node_modules/package-export-conditions/package.json b/test/node_modules/package-export-conditions/package.json new file mode 100644 index 0000000..a863960 --- /dev/null +++ b/test/node_modules/package-export-conditions/package.json @@ -0,0 +1,6 @@ +{ + "exports": { + "import": "./esm.mjs", + "require": "./cjs.js" + } +}