1# resolve <sup>[![Version Badge][2]][1]</sup> 2 3implements 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 16asynchronously resolve: 17 18```js 19var resolve = require('resolve/async'); // or, require('resolve') 20resolve('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 31synchronously resolve: 32 33```js 34var resolve = require('resolve/sync'); // or, `require('resolve').sync 35var res = resolve('tap', { basedir: __dirname }); 36console.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 47var resolve = require('resolve'); 48var async = require('resolve/async'); 49var sync = require('resolve/sync'); 50``` 51 52For 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 60Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`. 61 62options 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 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. 113This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag. 114**Note:** this property is currently `true` by default but it will be changed to 115`false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*. 116 117default `opts` values: 118 119```js 120{ 121 paths: [], 122 basedir: __dirname, 123 extensions: ['.js'], 124 includeCoreModules: true, 125 readFile: fs.readFile, 126 isFile: function isFile(file, cb) { 127 fs.stat(file, function (err, stat) { 128 if (!err) { 129 return cb(null, stat.isFile() || stat.isFIFO()); 130 } 131 if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false); 132 return cb(err); 133 }); 134 }, 135 isDirectory: function isDirectory(dir, cb) { 136 fs.stat(dir, function (err, stat) { 137 if (!err) { 138 return cb(null, stat.isDirectory()); 139 } 140 if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false); 141 return cb(err); 142 }); 143 }, 144 realpath: function realpath(file, cb) { 145 var realpath = typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath; 146 realpath(file, function (realPathErr, realPath) { 147 if (realPathErr && realPathErr.code !== 'ENOENT') cb(realPathErr); 148 else cb(null, realPathErr ? file : realPath); 149 }); 150 }, 151 readPackage: function defaultReadPackage(readFile, pkgfile, cb) { 152 readFile(pkgfile, function (readFileErr, body) { 153 if (readFileErr) cb(readFileErr); 154 else { 155 try { 156 var pkg = JSON.parse(body); 157 cb(null, pkg); 158 } catch (jsonErr) { 159 cb(null); 160 } 161 } 162 }); 163 }, 164 moduleDirectory: 'node_modules', 165 preserveSymlinks: true 166} 167``` 168 169## resolve.sync(id, opts) 170 171Synchronously resolve the module path string `id`, returning the result and 172throwing an error when `id` can't be resolved. 173 174options are: 175 176* opts.basedir - directory to begin resolving from 177 178* opts.extensions - array of file extensions to search in order 179 180* opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search 181 182* opts.readFileSync - how to read files synchronously 183 184* opts.isFile - function to synchronously test whether a file exists 185 186* opts.isDirectory - function to synchronously test whether a file exists and is a directory 187 188* opts.realpathSync - function to synchronously resolve a potential symlink to its real path 189 190* `opts.readPackageSync(readFileSync, pkgfile)` - function to synchronously read and parse a package.json file 191 * readFileSync - the passed `opts.readFileSync` or `fs.readFileSync` if not specified 192 * pkgfile - path to package.json 193 194* `opts.packageFilter(pkg, dir)` - transform the parsed package.json contents before looking at the "main" field 195 * pkg - package data 196 * dir - directory that contains package.json (Note: the second argument will change to "pkgfile" in v2) 197 198* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package 199 * pkg - package data 200 * path - the path being resolved 201 * relativePath - the path relative from the package.json location 202 * returns - a relative path that will be joined from the package.json location 203 204* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this) 205 206 For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function 207 * request - the import specifier being resolved 208 * start - lookup path 209 * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution 210 * opts - the resolution options 211 212* `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this) 213 * request - the import specifier being resolved 214 * start - lookup path 215 * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution 216 * opts - the resolution options 217 218* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"` 219 220* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving. 221This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag. 222**Note:** this property is currently `true` by default but it will be changed to 223`false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*. 224 225default `opts` values: 226 227```js 228{ 229 paths: [], 230 basedir: __dirname, 231 extensions: ['.js'], 232 includeCoreModules: true, 233 readFileSync: fs.readFileSync, 234 isFile: function isFile(file) { 235 try { 236 var stat = fs.statSync(file); 237 } catch (e) { 238 if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false; 239 throw e; 240 } 241 return stat.isFile() || stat.isFIFO(); 242 }, 243 isDirectory: function isDirectory(dir) { 244 try { 245 var stat = fs.statSync(dir); 246 } catch (e) { 247 if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false; 248 throw e; 249 } 250 return stat.isDirectory(); 251 }, 252 realpathSync: function realpathSync(file) { 253 try { 254 var realpath = typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync; 255 return realpath(file); 256 } catch (realPathErr) { 257 if (realPathErr.code !== 'ENOENT') { 258 throw realPathErr; 259 } 260 } 261 return file; 262 }, 263 readPackageSync: function defaultReadPackageSync(readFileSync, pkgfile) { 264 var body = readFileSync(pkgfile); 265 try { 266 var pkg = JSON.parse(body); 267 return pkg; 268 } catch (jsonErr) {} 269 }, 270 moduleDirectory: 'node_modules', 271 preserveSymlinks: true 272} 273``` 274 275# install 276 277With [npm](https://npmjs.org) do: 278 279```sh 280npm install resolve 281``` 282 283# license 284 285MIT 286 287[1]: https://npmjs.org/package/resolve 288[2]: https://versionbadg.es/browserify/resolve.svg 289[5]: https://david-dm.org/browserify/resolve.svg 290[6]: https://david-dm.org/browserify/resolve 291[7]: https://david-dm.org/browserify/resolve/dev-status.svg 292[8]: https://david-dm.org/browserify/resolve#info=devDependencies 293[11]: https://nodei.co/npm/resolve.png?downloads=true&stars=true 294[license-image]: https://img.shields.io/npm/l/resolve.svg 295[license-url]: LICENSE 296[downloads-image]: https://img.shields.io/npm/dm/resolve.svg 297[downloads-url]: https://npm-stat.com/charts.html?package=resolve 298[codecov-image]: https://codecov.io/gh/browserify/resolve/branch/main/graphs/badge.svg 299[codecov-url]: https://app.codecov.io/gh/browserify/resolve/ 300[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/browserify/resolve 301[actions-url]: https://github.com/browserify/resolve/actions 302