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
8 [![build status](https://secure.travis-ci.org/substack/node-resolve.png)](http://travis-ci.org/substack/node-resolve)
12 asynchronously resolve:
15 var resolve = require('resolve');
16 resolve('tap', { basedir: __dirname }, function (err, res) {
17 if (err) console.error(err)
23 $ node example/async.js
24 /home/substack/projects/node-resolve/node_modules/tap/lib/main.js
27 synchronously resolve:
30 var resolve = require('resolve');
31 var res = resolve.sync('tap', { basedir: __dirname });
36 $ node example/sync.js
37 /home/substack/projects/node-resolve/node_modules/tap/lib/main.js
43 var resolve = require('resolve')
46 ## resolve(id, opts={}, cb)
48 Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.
52 * opts.basedir - directory to begin resolving from
54 * opts.package - `package.json` data applicable to the module being loaded
56 * opts.extensions - array of file extensions to search in order
58 * opts.readFile - how to read files asynchronously
60 * opts.isFile - function to asynchronously test whether a file exists
62 * opts.packageFilter - transform the parsed package.json contents before looking
65 * opts.pathFilter(pkg, path, relativePath) - transform a path within a package
67 * path - the path being resolved
68 * relativePath - the path relative from the package.json location
69 * returns - a relative path that will be joined from the package.json location
71 * opts.paths - require.paths array to use if nothing is found on the normal
72 node_modules recursive walk (probably don't use this)
74 * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
76 default `opts` values:
82 extensions: [ '.js' ],
83 readFile: fs.readFile,
84 isFile: function (file, cb) {
85 fs.stat(file, function (err, stat) {
86 if (err && err.code === 'ENOENT') cb(null, false)
88 else cb(null, stat.isFile())
91 moduleDirectory: 'node_modules'
95 ## resolve.sync(id, opts)
97 Synchronously resolve the module path string `id`, returning the result and
98 throwing an error when `id` can't be resolved.
102 * opts.basedir - directory to begin resolving from
104 * opts.extensions - array of file extensions to search in order
106 * opts.readFile - how to read files synchronously
108 * opts.isFile - function to synchronously test whether a file exists
110 * `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json
111 * contents before looking at the "main" field
113 * opts.paths - require.paths array to use if nothing is found on the normal
114 node_modules recursive walk (probably don't use this)
116 * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
118 default `opts` values:
124 extensions: [ '.js' ],
125 readFileSync: fs.readFileSync,
126 isFile: function (file) {
127 try { return fs.statSync(file).isFile() }
128 catch (e) { return false }
130 moduleDirectory: 'node_modules'
134 ## resolve.isCore(pkg)
136 Return whether a package is in core.
140 With [npm](https://npmjs.org) do: