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