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

Function tryPackage

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

* Try to load a specifier as a package. * @param {string} requestPath The path to what we are trying to load * @param {string[]} exts File extensions to try appending in order to resolve the file * @param {boolean} isMain Whether the file is the main entry point of the app * @param {string} orig

(requestPath, exts, isMain, originalPath)

Source from the content-addressed store, hash-verified

546 * @returns {any}
547 */
548function tryPackage(requestPath, exts, isMain, originalPath) {
549 const { main: pkg, pjsonPath } = _readPackage(requestPath);
550
551 if (!pkg) {
552 return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
553 }
554
555 const filename = path.resolve(requestPath, pkg);
556 let actual = tryFile(filename, isMain) ||
557 tryExtensions(filename, exts, isMain) ||
558 tryExtensions(path.resolve(filename, 'index'), exts, isMain);
559 if (actual === false) {
560 actual = tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
561 if (!actual) {
562 // eslint-disable-next-line no-restricted-syntax
563 const err = new Error(
564 `Cannot find module '${filename}'. ` +
565 'Please verify that the package.json has a valid "main" entry',
566 );
567 err.code = 'MODULE_NOT_FOUND';
568 err.path = pjsonPath;
569 err.requestPath = originalPath;
570 // TODO(BridgeAR): Add the requireStack as well.
571 throw err;
572 } else {
573 process.emitWarning(
574 `Invalid 'main' field in '${pjsonPath}' of '${pkg}'. ` +
575 'Please either fix that or report it to the module author',
576 'DeprecationWarning',
577 'DEP0128',
578 );
579 }
580 }
581 return actual;
582}
583
584/**
585 * Check if the file exists and is not a directory if using `--preserve-symlinks` and `isMain` is false or

Callers 1

loader.jsFile · 0.85

Calls 4

_readPackageFunction · 0.85
tryExtensionsFunction · 0.85
tryFileFunction · 0.70
resolveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…