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