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

Function tryPackageMapResolveCJS

lib/internal/modules/cjs/loader.js:690–717  ·  view source on GitHub ↗

* Try to resolve using package map (if enabled via --experimental-package-map). * @param {string} request - The bare specifier * @param {Module} parent - The parent module * @param {Set } conditions - Export conditions * @returns {string|undefined}

(request, parent, conditions)

Source from the content-addressed store, hash-verified

688 * @returns {string|undefined}
689 */
690function tryPackageMapResolveCJS(request, parent, conditions) {
691 if (!hasPackageMap()) { return undefined; }
692
693 const parentPath = trySelfParentPath(parent);
694 if (!parentPath) { return undefined; }
695
696 const mapped = packageMapResolve(request, parentPath);
697 if (mapped === undefined) {
698 const requireStack = getRequireStack(parent);
699 const message = getRequireStackMessage(request, requireStack);
700 // eslint-disable-next-line no-restricted-syntax
701 const err = new Error(message);
702 setOwnProperty(err, 'code', 'MODULE_NOT_FOUND');
703 setOwnProperty(err, 'requireStack', requireStack);
704 throw err;
705 }
706
707 const { packagePath, subpath } = mapped;
708
709 const packageJSONPath = path.resolve(packagePath, 'package.json');
710 const pkg = packageJsonReader.read(packageJSONPath);
711 if (pkg.exports !== undefined) {
712 return resolveExpansion(subpath, packageJSONPath, pkg, parentPath, conditions);
713 }
714
715 // No exports - try standard path resolution within the package
716 return Module._findPath(subpath, [packagePath], false, conditions);
717}
718
719function resolveExpansion(expansion, pkgJsonPath, pkgData, parentPath, conditions) {
720 const pkgPath = path.dirname(pkgJsonPath);

Callers 1

loader.jsFile · 0.85

Calls 9

hasPackageMapFunction · 0.85
trySelfParentPathFunction · 0.85
packageMapResolveFunction · 0.85
getRequireStackFunction · 0.85
getRequireStackMessageFunction · 0.85
setOwnPropertyFunction · 0.85
resolveExpansionFunction · 0.85
resolveMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…