getModulePath.cjs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const process = require('node:process');
  5. const globalModules = require('global-modules');
  6. const errors = require('./errors.cjs');
  7. const resolveSilent = require('./resolveSilent.cjs');
  8. /**
  9. * @param {string} basedir
  10. * @param {string} lookup
  11. * @param {string} [cwd]
  12. * @returns {string}
  13. */
  14. function getModulePath(basedir, lookup, cwd = process.cwd()) {
  15. // 1. Try to resolve from the provided directory
  16. // 2. Try to resolve from `cwd` or `process.cwd()`
  17. // 3. Try to resolve from global `node_modules` directory
  18. let path = resolveSilent(basedir, lookup);
  19. if (!path) {
  20. path = resolveSilent(cwd, lookup);
  21. }
  22. if (!path) {
  23. path = resolveSilent(globalModules, lookup);
  24. }
  25. if (!path) {
  26. throw new errors.ConfigurationError(
  27. `Could not find "${lookup}". Do you need to install the package or use the "configBasedir" option?`,
  28. );
  29. }
  30. return path;
  31. }
  32. module.exports = getModulePath;