1 | # resolve
|
---|
2 |
|
---|
3 | implements the [node `require.resolve()`
|
---|
4 | algorithm](https://nodejs.org/api/modules.html#modules_all_together)
|
---|
5 | such that you can `require.resolve()` on behalf of a file asynchronously and
|
---|
6 | synchronously
|
---|
7 |
|
---|
8 | [![build status](https://secure.travis-ci.org/browserify/resolve.png)](http://travis-ci.org/browserify/resolve)
|
---|
9 |
|
---|
10 | # example
|
---|
11 |
|
---|
12 | asynchronously resolve:
|
---|
13 |
|
---|
14 | ```js
|
---|
15 | var resolve = require('resolve');
|
---|
16 | resolve('tap', { basedir: __dirname }, function (err, res) {
|
---|
17 | if (err) console.error(err);
|
---|
18 | else console.log(res);
|
---|
19 | });
|
---|
20 | ```
|
---|
21 |
|
---|
22 | ```
|
---|
23 | $ node example/async.js
|
---|
24 | /home/substack/projects/node-resolve/node_modules/tap/lib/main.js
|
---|
25 | ```
|
---|
26 |
|
---|
27 | synchronously resolve:
|
---|
28 |
|
---|
29 | ```js
|
---|
30 | var resolve = require('resolve');
|
---|
31 | var res = resolve.sync('tap', { basedir: __dirname });
|
---|
32 | console.log(res);
|
---|
33 | ```
|
---|
34 |
|
---|
35 | ```
|
---|
36 | $ node example/sync.js
|
---|
37 | /home/substack/projects/node-resolve/node_modules/tap/lib/main.js
|
---|
38 | ```
|
---|
39 |
|
---|
40 | # methods
|
---|
41 |
|
---|
42 | ```js
|
---|
43 | var resolve = require('resolve');
|
---|
44 | ```
|
---|
45 |
|
---|
46 | For both the synchronous and asynchronous methods, errors may have any of the following `err.code` values:
|
---|
47 |
|
---|
48 | - `MODULE_NOT_FOUND`: the given path string (`id`) could not be resolved to a module
|
---|
49 | - `INVALID_BASEDIR`: the specified `opts.basedir` doesn't exist, or is not a directory
|
---|
50 | - `INVALID_PACKAGE_MAIN`: a `package.json` was encountered with an invalid `main` property (eg. not a string)
|
---|
51 |
|
---|
52 | ## resolve(id, opts={}, cb)
|
---|
53 |
|
---|
54 | Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.
|
---|
55 |
|
---|
56 | options are:
|
---|
57 |
|
---|
58 | * opts.basedir - directory to begin resolving from
|
---|
59 |
|
---|
60 | * opts.package - `package.json` data applicable to the module being loaded
|
---|
61 |
|
---|
62 | * opts.extensions - array of file extensions to search in order
|
---|
63 |
|
---|
64 | * opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search
|
---|
65 |
|
---|
66 | * opts.readFile - how to read files asynchronously
|
---|
67 |
|
---|
68 | * opts.isFile - function to asynchronously test whether a file exists
|
---|
69 |
|
---|
70 | * opts.isDirectory - function to asynchronously test whether a directory exists
|
---|
71 |
|
---|
72 | * opts.realpath - function to asynchronously resolve a potential symlink to its real path
|
---|
73 |
|
---|
74 | * `opts.readPackage(readFile, pkgfile, cb)` - function to asynchronously read and parse a package.json file
|
---|
75 | * readFile - the passed `opts.readFile` or `fs.readFile` if not specified
|
---|
76 | * pkgfile - path to package.json
|
---|
77 | * cb - callback
|
---|
78 |
|
---|
79 | * `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the "main" field
|
---|
80 | * pkg - package data
|
---|
81 | * pkgfile - path to package.json
|
---|
82 | * dir - directory for package.json
|
---|
83 |
|
---|
84 | * `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
|
---|
85 | * pkg - package data
|
---|
86 | * path - the path being resolved
|
---|
87 | * relativePath - the path relative from the package.json location
|
---|
88 | * returns - a relative path that will be joined from the package.json location
|
---|
89 |
|
---|
90 | * opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
|
---|
91 |
|
---|
92 | For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
|
---|
93 | * request - the import specifier being resolved
|
---|
94 | * start - lookup path
|
---|
95 | * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
|
---|
96 | * opts - the resolution options
|
---|
97 |
|
---|
98 | * `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)
|
---|
99 | * request - the import specifier being resolved
|
---|
100 | * start - lookup path
|
---|
101 | * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
|
---|
102 | * opts - the resolution options
|
---|
103 |
|
---|
104 | * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
|
---|
105 |
|
---|
106 | * opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
|
---|
107 | This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
|
---|
108 | **Note:** this property is currently `true` by default but it will be changed to
|
---|
109 | `false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.
|
---|
110 |
|
---|
111 | default `opts` values:
|
---|
112 |
|
---|
113 | ```js
|
---|
114 | {
|
---|
115 | paths: [],
|
---|
116 | basedir: __dirname,
|
---|
117 | extensions: ['.js'],
|
---|
118 | includeCoreModules: true,
|
---|
119 | readFile: fs.readFile,
|
---|
120 | isFile: function isFile(file, cb) {
|
---|
121 | fs.stat(file, function (err, stat) {
|
---|
122 | if (!err) {
|
---|
123 | return cb(null, stat.isFile() || stat.isFIFO());
|
---|
124 | }
|
---|
125 | if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
|
---|
126 | return cb(err);
|
---|
127 | });
|
---|
128 | },
|
---|
129 | isDirectory: function isDirectory(dir, cb) {
|
---|
130 | fs.stat(dir, function (err, stat) {
|
---|
131 | if (!err) {
|
---|
132 | return cb(null, stat.isDirectory());
|
---|
133 | }
|
---|
134 | if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
|
---|
135 | return cb(err);
|
---|
136 | });
|
---|
137 | },
|
---|
138 | realpath: function realpath(file, cb) {
|
---|
139 | var realpath = typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath;
|
---|
140 | realpath(file, function (realPathErr, realPath) {
|
---|
141 | if (realPathErr && realPathErr.code !== 'ENOENT') cb(realPathErr);
|
---|
142 | else cb(null, realPathErr ? file : realPath);
|
---|
143 | });
|
---|
144 | },
|
---|
145 | readPackage: function defaultReadPackage(readFile, pkgfile, cb) {
|
---|
146 | readFile(pkgfile, function (readFileErr, body) {
|
---|
147 | if (readFileErr) cb(readFileErr);
|
---|
148 | else {
|
---|
149 | try {
|
---|
150 | var pkg = JSON.parse(body);
|
---|
151 | cb(null, pkg);
|
---|
152 | } catch (jsonErr) {
|
---|
153 | cb(null);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | });
|
---|
157 | },
|
---|
158 | moduleDirectory: 'node_modules',
|
---|
159 | preserveSymlinks: true
|
---|
160 | }
|
---|
161 | ```
|
---|
162 |
|
---|
163 | ## resolve.sync(id, opts)
|
---|
164 |
|
---|
165 | Synchronously resolve the module path string `id`, returning the result and
|
---|
166 | throwing an error when `id` can't be resolved.
|
---|
167 |
|
---|
168 | options are:
|
---|
169 |
|
---|
170 | * opts.basedir - directory to begin resolving from
|
---|
171 |
|
---|
172 | * opts.extensions - array of file extensions to search in order
|
---|
173 |
|
---|
174 | * opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search
|
---|
175 |
|
---|
176 | * opts.readFileSync - how to read files synchronously
|
---|
177 |
|
---|
178 | * opts.isFile - function to synchronously test whether a file exists
|
---|
179 |
|
---|
180 | * opts.isDirectory - function to synchronously test whether a directory exists
|
---|
181 |
|
---|
182 | * opts.realpathSync - function to synchronously resolve a potential symlink to its real path
|
---|
183 |
|
---|
184 | * `opts.readPackageSync(readFileSync, pkgfile)` - function to synchronously read and parse a package.json file
|
---|
185 | * readFileSync - the passed `opts.readFileSync` or `fs.readFileSync` if not specified
|
---|
186 | * pkgfile - path to package.json
|
---|
187 |
|
---|
188 | * `opts.packageFilter(pkg, dir)` - transform the parsed package.json contents before looking at the "main" field
|
---|
189 | * pkg - package data
|
---|
190 | * dir - directory for package.json (Note: the second argument will change to "pkgfile" in v2)
|
---|
191 |
|
---|
192 | * `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
|
---|
193 | * pkg - package data
|
---|
194 | * path - the path being resolved
|
---|
195 | * relativePath - the path relative from the package.json location
|
---|
196 | * returns - a relative path that will be joined from the package.json location
|
---|
197 |
|
---|
198 | * opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
|
---|
199 |
|
---|
200 | For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
|
---|
201 | * request - the import specifier being resolved
|
---|
202 | * start - lookup path
|
---|
203 | * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
|
---|
204 | * opts - the resolution options
|
---|
205 |
|
---|
206 | * `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)
|
---|
207 | * request - the import specifier being resolved
|
---|
208 | * start - lookup path
|
---|
209 | * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
|
---|
210 | * opts - the resolution options
|
---|
211 |
|
---|
212 | * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
|
---|
213 |
|
---|
214 | * opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
|
---|
215 | This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
|
---|
216 | **Note:** this property is currently `true` by default but it will be changed to
|
---|
217 | `false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.
|
---|
218 |
|
---|
219 | default `opts` values:
|
---|
220 |
|
---|
221 | ```js
|
---|
222 | {
|
---|
223 | paths: [],
|
---|
224 | basedir: __dirname,
|
---|
225 | extensions: ['.js'],
|
---|
226 | includeCoreModules: true,
|
---|
227 | readFileSync: fs.readFileSync,
|
---|
228 | isFile: function isFile(file) {
|
---|
229 | try {
|
---|
230 | var stat = fs.statSync(file);
|
---|
231 | } catch (e) {
|
---|
232 | if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
|
---|
233 | throw e;
|
---|
234 | }
|
---|
235 | return stat.isFile() || stat.isFIFO();
|
---|
236 | },
|
---|
237 | isDirectory: function isDirectory(dir) {
|
---|
238 | try {
|
---|
239 | var stat = fs.statSync(dir);
|
---|
240 | } catch (e) {
|
---|
241 | if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
|
---|
242 | throw e;
|
---|
243 | }
|
---|
244 | return stat.isDirectory();
|
---|
245 | },
|
---|
246 | realpathSync: function realpathSync(file) {
|
---|
247 | try {
|
---|
248 | var realpath = typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;
|
---|
249 | return realpath(file);
|
---|
250 | } catch (realPathErr) {
|
---|
251 | if (realPathErr.code !== 'ENOENT') {
|
---|
252 | throw realPathErr;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | return file;
|
---|
256 | },
|
---|
257 | readPackageSync: function defaultReadPackageSync(readFileSync, pkgfile) {
|
---|
258 | var body = readFileSync(pkgfile);
|
---|
259 | try {
|
---|
260 | var pkg = JSON.parse(body);
|
---|
261 | return pkg;
|
---|
262 | } catch (jsonErr) {}
|
---|
263 | },
|
---|
264 | moduleDirectory: 'node_modules',
|
---|
265 | preserveSymlinks: true
|
---|
266 | }
|
---|
267 | ```
|
---|
268 |
|
---|
269 | # install
|
---|
270 |
|
---|
271 | With [npm](https://npmjs.org) do:
|
---|
272 |
|
---|
273 | ```sh
|
---|
274 | npm install resolve
|
---|
275 | ```
|
---|
276 |
|
---|
277 | # license
|
---|
278 |
|
---|
279 | MIT
|
---|