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

Function trySelf

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

* Attempt to resolve a module request using the parent module package metadata. * @param {string} parentPath The path of the parent module * @param {string} request The module request to resolve * @param {unknown} conditions * @returns {false|string}

(parentPath, request, conditions)

Source from the content-addressed store, hash-verified

661 * @returns {false|string}
662 */
663function trySelf(parentPath, request, conditions) {
664 if (!parentPath) { return false; }
665
666 const pkg = packageJsonReader.getNearestParentPackageJSON(parentPath);
667 if (pkg?.data.exports === undefined || pkg.data.name === undefined) {
668 return false;
669 }
670
671 let expansion;
672 if (request === pkg.data.name) {
673 expansion = '.';
674 } else if (StringPrototypeStartsWith(request, `${pkg.data.name}/`)) {
675 expansion = '.' + StringPrototypeSlice(request, pkg.data.name.length);
676 } else {
677 return false;
678 }
679
680 return resolveExpansion(expansion, pkg.path, pkg.data, parentPath, conditions);
681}
682
683/**
684 * Try to resolve using package map (if enabled via --experimental-package-map).

Callers 1

loader.jsFile · 0.85

Calls 2

resolveExpansionFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…