getModulePath.mjs 869 B

1234567891011121314151617181920212223242526272829303132333435
  1. import process from 'node:process';
  2. import globalModules from 'global-modules';
  3. import { ConfigurationError } from './errors.mjs';
  4. import resolveSilent from './resolveSilent.mjs';
  5. /**
  6. * @param {string} basedir
  7. * @param {string} lookup
  8. * @param {string} [cwd]
  9. * @returns {string}
  10. */
  11. export default function getModulePath(basedir, lookup, cwd = process.cwd()) {
  12. // 1. Try to resolve from the provided directory
  13. // 2. Try to resolve from `cwd` or `process.cwd()`
  14. // 3. Try to resolve from global `node_modules` directory
  15. let path = resolveSilent(basedir, lookup);
  16. if (!path) {
  17. path = resolveSilent(cwd, lookup);
  18. }
  19. if (!path) {
  20. path = resolveSilent(globalModules, lookup);
  21. }
  22. if (!path) {
  23. throw new ConfigurationError(
  24. `Could not find "${lookup}". Do you need to install the package or use the "configBasedir" option?`,
  25. );
  26. }
  27. return path;
  28. }