readme.markdown
1# resolve
2
3implements the [node `require.resolve()`
4algorithm](https://nodejs.org/api/modules.html#modules_all_together)
5such that you can `require.resolve()` on behalf of a file asynchronously and
6synchronously
7
8[](http://travis-ci.org/browserify/node-resolve)
9
10# example
11
12asynchronously resolve:
13
14```js
15var resolve = require('resolve');
16resolve('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
27synchronously resolve:
28
29```js
30var resolve = require('resolve');
31var res = resolve.sync('tap', { basedir: __dirname });
32console.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
43var resolve = require('resolve');
44```
45
46## resolve(id, opts={}, cb)
47
48Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.
49
50options are:
51
52* opts.basedir - directory to begin resolving from
53
54* opts.package - `package.json` data applicable to the module being loaded
55
56* opts.extensions - array of file extensions to search in order
57
58* opts.readFile - how to read files asynchronously
59
60* opts.isFile - function to asynchronously test whether a file exists
61
62* `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json contents before looking at the "main" field
63 * pkg - package data
64 * pkgfile - path to package.json
65
66* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
67 * pkg - package data
68 * path - the path being resolved
69 * relativePath - the path relative from the package.json location
70 * returns - a relative path that will be joined from the package.json location
71
72* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
73
74 For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
75 * request - the import specifier being resolved
76 * start - lookup path
77 * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
78 * opts - the resolution options
79
80* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
81
82* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
83This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
84**Note:** this property is currently `true` by default but it will be changed to
85`false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.
86
87default `opts` values:
88
89```js
90{
91 paths: [],
92 basedir: __dirname,
93 extensions: ['.js'],
94 readFile: fs.readFile,
95 isFile: function isFile(file, cb) {
96 fs.stat(file, function (err, stat) {
97 if (!err) {
98 return cb(null, stat.isFile() || stat.isFIFO());
99 }
100 if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
101 return cb(err);
102 });
103 },
104 moduleDirectory: 'node_modules',
105 preserveSymlinks: true
106}
107```
108
109## resolve.sync(id, opts)
110
111Synchronously resolve the module path string `id`, returning the result and
112throwing an error when `id` can't be resolved.
113
114options are:
115
116* opts.basedir - directory to begin resolving from
117
118* opts.extensions - array of file extensions to search in order
119
120* opts.readFile - how to read files synchronously
121
122* opts.isFile - function to synchronously test whether a file exists
123
124* `opts.packageFilter(pkg, dir)` - transform the parsed package.json contents before looking at the "main" field
125 * pkg - package data
126 * dir - directory for package.json (Note: the second argument will change to "pkgfile" in v2)
127
128* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
129 * pkg - package data
130 * path - the path being resolved
131 * relativePath - the path relative from the package.json location
132 * returns - a relative path that will be joined from the package.json location
133
134* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
135
136* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
137
138* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
139This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
140**Note:** this property is currently `true` by default but it will be changed to
141`false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.
142
143default `opts` values:
144
145```js
146{
147 paths: [],
148 basedir: __dirname,
149 extensions: ['.js'],
150 readFileSync: fs.readFileSync,
151 isFile: function isFile(file) {
152 try {
153 var stat = fs.statSync(file);
154 } catch (e) {
155 if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
156 throw e;
157 }
158 return stat.isFile() || stat.isFIFO();
159 },
160 moduleDirectory: 'node_modules',
161 preserveSymlinks: true
162}
163```
164
165## resolve.isCore(pkg)
166
167Return whether a package is in core.
168
169# install
170
171With [npm](https://npmjs.org) do:
172
173```sh
174npm install resolve
175```
176
177# license
178
179MIT
180