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