5e1aea331767debaabc5272732586cd6efca142d
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / resolve / readme.markdown
1 # resolve
2
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
6 synchronously
7
8 [![build status](https://secure.travis-ci.org/browserify/resolve.png)](http://travis-ci.org/browserify/resolve)
9
10 # example
11
12 asynchronously resolve:
13
14 ```js
15 var resolve = require('resolve');
16 resolve('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
27 synchronously resolve:
28
29 ```js
30 var resolve = require('resolve');
31 var res = resolve.sync('tap', { basedir: __dirname });
32 console.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
43 var resolve = require('resolve');
44 ```
45
46 ## resolve(id, opts={}, cb)
47
48 Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.
49
50 options 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.isDirectory - function to asynchronously test whether a directory exists
63
64 * opts.realpath - function to asynchronously resolve a potential symlink to its real path
65
66 * `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the "main" field
67   * pkg - package data
68   * pkgfile - path to package.json
69   * dir - directory for package.json
70
71 * `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
72   * pkg - package data
73   * path - the path being resolved
74   * relativePath - the path relative from the package.json location
75   * returns - a relative path that will be joined from the package.json location
76
77 * opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
78
79   For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
80     * request - the import specifier being resolved
81     * start - lookup path
82     * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
83     * opts - the resolution options
84
85 * `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)
86     * request - the import specifier being resolved
87     * start - lookup path
88     * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
89     * opts - the resolution options
90
91 * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
92
93 * opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
94 This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
95 **Note:** this property is currently `true` by default but it will be changed to
96 `false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.
97
98 default `opts` values:
99
100 ```js
101 {
102     paths: [],
103     basedir: __dirname,
104     extensions: ['.js'],
105     readFile: fs.readFile,
106     isFile: function isFile(file, cb) {
107         fs.stat(file, function (err, stat) {
108             if (!err) {
109                 return cb(null, stat.isFile() || stat.isFIFO());
110             }
111             if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
112             return cb(err);
113         });
114     },
115     isDirectory: function isDirectory(dir, cb) {
116         fs.stat(dir, function (err, stat) {
117             if (!err) {
118                 return cb(null, stat.isDirectory());
119             }
120             if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
121             return cb(err);
122         });
123     },
124     realpath: function realpath(file, cb) {
125        var realpath = typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath;
126        realpath(file, function (realPathErr, realPath) {
127            if (realPathErr && realPathErr.code !== 'ENOENT') cb(realPathErr);
128            else cb(null, realPathErr ? file : realPath);
129        });
130    },
131     moduleDirectory: 'node_modules',
132     preserveSymlinks: true
133 }
134 ```
135
136 ## resolve.sync(id, opts)
137
138 Synchronously resolve the module path string `id`, returning the result and
139 throwing an error when `id` can't be resolved.
140
141 options are:
142
143 * opts.basedir - directory to begin resolving from
144
145 * opts.extensions - array of file extensions to search in order
146
147 * opts.readFile - how to read files synchronously
148
149 * opts.isFile - function to synchronously test whether a file exists
150
151 * opts.isDirectory - function to synchronously test whether a directory exists
152
153 * opts.realpathSync - function to synchronously resolve a potential symlink to its real path
154
155 * `opts.packageFilter(pkg, dir)` - transform the parsed package.json contents before looking at the "main" field
156   * pkg - package data
157   * dir - directory for package.json (Note: the second argument will change to "pkgfile" in v2)
158
159 * `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
160   * pkg - package data
161   * path - the path being resolved
162   * relativePath - the path relative from the package.json location
163   * returns - a relative path that will be joined from the package.json location
164
165 * opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
166
167   For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
168     * request - the import specifier being resolved
169     * start - lookup path
170     * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
171     * opts - the resolution options
172
173 * `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)
174     * request - the import specifier being resolved
175     * start - lookup path
176     * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
177     * opts - the resolution options
178
179 * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
180
181 * opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
182 This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
183 **Note:** this property is currently `true` by default but it will be changed to
184 `false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.
185
186 default `opts` values:
187
188 ```js
189 {
190     paths: [],
191     basedir: __dirname,
192     extensions: ['.js'],
193     readFileSync: fs.readFileSync,
194     isFile: function isFile(file) {
195         try {
196             var stat = fs.statSync(file);
197         } catch (e) {
198             if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
199             throw e;
200         }
201         return stat.isFile() || stat.isFIFO();
202     },
203     isDirectory: function isDirectory(dir) {
204         try {
205             var stat = fs.statSync(dir);
206         } catch (e) {
207             if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
208             throw e;
209         }
210         return stat.isDirectory();
211     },
212     realpathSync: function realpathSync(file) {
213         try {
214             var realpath = typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;
215             return realpath(file);
216         } catch (realPathErr) {
217             if (realPathErr.code !== 'ENOENT') {
218                 throw realPathErr;
219             }
220         }
221         return file;
222     },
223     moduleDirectory: 'node_modules',
224     preserveSymlinks: true
225 }
226 ```
227
228 ## resolve.isCore(pkg)
229
230 Return whether a package is in core.
231
232 # install
233
234 With [npm](https://npmjs.org) do:
235
236 ```sh
237 npm install resolve
238 ```
239
240 # license
241
242 MIT