You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

4 years ago
  1. # resolve-from [![Build Status](https://travis-ci.org/sindresorhus/resolve-from.svg?branch=master)](https://travis-ci.org/sindresorhus/resolve-from)
  2. > Resolve the path of a module like [`require.resolve()`](https://nodejs.org/api/globals.html#globals_require_resolve) but from a given path
  3. ## Install
  4. ```
  5. $ npm install --save resolve-from
  6. ```
  7. ## Usage
  8. ```js
  9. const resolveFrom = require('resolve-from');
  10. // There is a file at `./foo/bar.js`
  11. resolveFrom('foo', './bar');
  12. //=> '/Users/sindresorhus/dev/test/foo/bar.js'
  13. ```
  14. ## API
  15. ### resolveFrom(fromDir, moduleId)
  16. Like `require()`, throws when the module can't be found.
  17. ### resolveFrom.silent(fromDir, moduleId)
  18. Returns `null` instead of throwing when the module can't be found.
  19. #### fromDir
  20. Type: `string`
  21. Directory to resolve from.
  22. #### moduleId
  23. Type: `string`
  24. What you would use in `require()`.
  25. ## Tip
  26. Create a partial using a bound function if you want to resolve from the same `fromDir` multiple times:
  27. ```js
  28. const resolveFromFoo = resolveFrom.bind(null, 'foo');
  29. resolveFromFoo('./bar');
  30. resolveFromFoo('./baz');
  31. ```
  32. ## Related
  33. - [resolve-cwd](https://github.com/sindresorhus/resolve-cwd) - Resolve the path of a module from the current working directory
  34. - [req-from](https://github.com/sindresorhus/req-from) - Require a module from a given path
  35. - [req-cwd](https://github.com/sindresorhus/req-cwd) - Require a module from the current working directory
  36. - [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point
  37. - [lazy-req](https://github.com/sindresorhus/lazy-req) - Require modules lazily
  38. ## License
  39. MIT © [Sindre Sorhus](https://sindresorhus.com)