1 | # resolve-from [![Build Status](https://travis-ci.org/sindresorhus/resolve-from.svg?branch=master)](https://travis-ci.org/sindresorhus/resolve-from)
|
---|
2 |
|
---|
3 | > Resolve the path of a module like [`require.resolve()`](https://nodejs.org/api/globals.html#globals_require_resolve) but from a given path
|
---|
4 |
|
---|
5 |
|
---|
6 | ## Install
|
---|
7 |
|
---|
8 | ```
|
---|
9 | $ npm install resolve-from
|
---|
10 | ```
|
---|
11 |
|
---|
12 |
|
---|
13 | ## Usage
|
---|
14 |
|
---|
15 | ```js
|
---|
16 | const resolveFrom = require('resolve-from');
|
---|
17 |
|
---|
18 | // There is a file at `./foo/bar.js`
|
---|
19 |
|
---|
20 | resolveFrom('foo', './bar');
|
---|
21 | //=> '/Users/sindresorhus/dev/test/foo/bar.js'
|
---|
22 | ```
|
---|
23 |
|
---|
24 |
|
---|
25 | ## API
|
---|
26 |
|
---|
27 | ### resolveFrom(fromDir, moduleId)
|
---|
28 |
|
---|
29 | Like `require()`, throws when the module can't be found.
|
---|
30 |
|
---|
31 | ### resolveFrom.silent(fromDir, moduleId)
|
---|
32 |
|
---|
33 | Returns `null` instead of throwing when the module can't be found.
|
---|
34 |
|
---|
35 | #### fromDir
|
---|
36 |
|
---|
37 | Type: `string`
|
---|
38 |
|
---|
39 | Directory to resolve from.
|
---|
40 |
|
---|
41 | #### moduleId
|
---|
42 |
|
---|
43 | Type: `string`
|
---|
44 |
|
---|
45 | What you would use in `require()`.
|
---|
46 |
|
---|
47 |
|
---|
48 | ## Tip
|
---|
49 |
|
---|
50 | Create a partial using a bound function if you want to resolve from the same `fromDir` multiple times:
|
---|
51 |
|
---|
52 | ```js
|
---|
53 | const resolveFromFoo = resolveFrom.bind(null, 'foo');
|
---|
54 |
|
---|
55 | resolveFromFoo('./bar');
|
---|
56 | resolveFromFoo('./baz');
|
---|
57 | ```
|
---|
58 |
|
---|
59 |
|
---|
60 | ## Related
|
---|
61 |
|
---|
62 | - [resolve-cwd](https://github.com/sindresorhus/resolve-cwd) - Resolve the path of a module from the current working directory
|
---|
63 | - [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path
|
---|
64 | - [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory
|
---|
65 | - [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point
|
---|
66 | - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily
|
---|
67 | - [resolve-global](https://github.com/sindresorhus/resolve-global) - Resolve the path of a globally installed module
|
---|
68 |
|
---|
69 |
|
---|
70 | ## License
|
---|
71 |
|
---|
72 | MIT © [Sindre Sorhus](https://sindresorhus.com)
|
---|