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.

73 lines
2.2 KiB

4 years ago
  1. # is-resolvable
  2. [![npm version](https://img.shields.io/npm/v/is-resolvable.svg)](https://www.npmjs.com/package/is-resolvable)
  3. [![Build Status](https://travis-ci.org/shinnn/is-resolvable.svg?branch=master)](https://travis-ci.org/shinnn/is-resolvable)
  4. [![Build status](https://ci.appveyor.com/api/projects/status/ww1cdpignehlasbs?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/is-resolvable)
  5. [![Coverage Status](https://img.shields.io/coveralls/shinnn/is-resolvable.svg)](https://coveralls.io/r/shinnn/is-resolvable)
  6. A [Node.js](https://nodejs.org/) module to check if a given module ID is resolvable with [`require()`](https://nodejs.org/api/globals.html#globals_require)
  7. ```javascript
  8. const isResolvable = require('is-resolvable');
  9. isResolvable('fs'); //=> true
  10. isResolvable('path'); //=> true
  11. // When ./index.js exists
  12. isResolvable('./index.js') //=> true
  13. isResolvable('./index') //=> true
  14. isResolvable('.') //=> true
  15. ```
  16. ## Installation
  17. [Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm).
  18. ```
  19. npm install is-resolvable
  20. ```
  21. ## API
  22. ```javascript
  23. const isResolvable = require('is-resolvable');
  24. ```
  25. ### isResolvable(*moduleId* [, *options*])
  26. *moduleId*: `string` (module ID)
  27. *options*: `Object` ([`require.resolve`](https://nodejs.org/api/modules.html#modules_require_resolve_request_options) options)
  28. Return: `boolean`
  29. It returns `true` if `require()` can load a file form a given module ID, otherwise `false`.
  30. ```javascript
  31. const isResolvable = require('is-resolvable');
  32. // When ./foo.json exists
  33. isResolvable('./foo.json'); //=> true
  34. isResolvable('./foo'); //=> true
  35. isResolvable('./foo.js'); //=> false
  36. // When `eslint` module is installed but `jshint` isn't
  37. isResolvable('eslint'); //=> true
  38. isResolvable('jshint'); //=> false
  39. // When `lodash` module is installed
  40. isResolvable('lodash/isObject'); //=> true
  41. isResolvable('lodash/fp/reject.js'); //=> true
  42. ```
  43. The second argument accepts an options object for `require.resolve()`.
  44. ```javascript
  45. // When ./bar/baz.js exists
  46. isResolvable('./baz.js'); //=> false
  47. isResolvable('./baz.js', {paths: ['bar']}); //=> true
  48. ```
  49. ## License
  50. [ISC License](./LICENSE) © 2018 Shinnosuke Watanabe