MCPcopy Index your code
hub / github.com/nodejs/node / packageResolve

Function packageResolve

lib/internal/modules/esm/resolve.js:762–800  ·  view source on GitHub ↗

* Resolves a package specifier to a URL. * @param {string} specifier - The package specifier to resolve. * @param {string | URL | undefined} base - The base URL to use for resolution. * @param {Set } conditions - An object containing the conditions for resolution. * @returns {URL} - The r

(specifier, base, conditions)

Source from the content-addressed store, hash-verified

760 * @returns {URL} - The resolved URL.
761 */
762function packageResolve(specifier, base, conditions) {
763 // TODO(@anonrig): Move this to a C++ function.
764 if (BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
765 return new URL('node:' + specifier);
766 }
767
768 let packageJSONUrl, packageJSONPath, packageSubpath;
769
770 if (hasPackageMap()) {
771 // Package map is enabled - use it exclusively
772 const parentPath = fileURLToPath(base);
773 const mapped = packageMapResolve(specifier, parentPath);
774 if (mapped === undefined) {
775 throw new ERR_MODULE_NOT_FOUND(specifier, fileURLToPath(base), null);
776 }
777 const { packagePath, subpath } = mapped;
778 packageJSONPath = join(packagePath, 'package.json');
779 packageJSONUrl = pathToFileURL(packageJSONPath);
780 packageSubpath = subpath;
781 } else {
782 // Standard node_modules resolution
783 ({ packageJSONUrl, packageJSONPath, packageSubpath } =
784 packageJsonReader.getPackageJSONURL(specifier, base));
785 }
786
787 const packageConfig = packageJsonReader.read(packageJSONPath, {
788 __proto__: null, specifier, base, isESM: true,
789 });
790
791 if (packageConfig.exports != null) {
792 return packageExportsResolve(
793 packageJSONUrl, packageSubpath, packageConfig, base, conditions);
794 }
795 if (packageSubpath === '.') {
796 return legacyMainResolve(packageJSONUrl, packageConfig, base);
797 }
798
799 return new URL(packageSubpath, packageJSONUrl);
800}
801
802/**
803 * Checks if a specifier is a bare specifier.

Callers 2

moduleResolveFunction · 0.85

Calls 9

hasPackageMapFunction · 0.85
fileURLToPathFunction · 0.85
packageMapResolveFunction · 0.85
packageExportsResolveFunction · 0.85
legacyMainResolveFunction · 0.85
joinFunction · 0.50
pathToFileURLFunction · 0.50
readMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…