Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

250 rader
8.9 KiB

4 år sedan
  1. # resolve
  2. implements the [node `require.resolve()`
  3. algorithm](https://nodejs.org/api/modules.html#modules_all_together)
  4. such that you can `require.resolve()` on behalf of a file asynchronously and
  5. synchronously
  6. [![build status](https://secure.travis-ci.org/browserify/resolve.png)](http://travis-ci.org/browserify/resolve)
  7. # example
  8. asynchronously resolve:
  9. ```js
  10. var resolve = require('resolve');
  11. resolve('tap', { basedir: __dirname }, function (err, res) {
  12. if (err) console.error(err);
  13. else console.log(res);
  14. });
  15. ```
  16. ```
  17. $ node example/async.js
  18. /home/substack/projects/node-resolve/node_modules/tap/lib/main.js
  19. ```
  20. synchronously resolve:
  21. ```js
  22. var resolve = require('resolve');
  23. var res = resolve.sync('tap', { basedir: __dirname });
  24. console.log(res);
  25. ```
  26. ```
  27. $ node example/sync.js
  28. /home/substack/projects/node-resolve/node_modules/tap/lib/main.js
  29. ```
  30. # methods
  31. ```js
  32. var resolve = require('resolve');
  33. ```
  34. For both the synchronous and asynchronous methods, errors may have any of the following `err.code` values:
  35. - `MODULE_NOT_FOUND`: the given path string (`id`) could not be resolved to a module
  36. - `INVALID_BASEDIR`: the specified `opts.basedir` doesn't exist, or is not a directory
  37. - `INVALID_PACKAGE_MAIN`: a `package.json` was encountered with an invalid `main` property (eg. not a string)
  38. ## resolve(id, opts={}, cb)
  39. Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.
  40. options are:
  41. * opts.basedir - directory to begin resolving from
  42. * opts.package - `package.json` data applicable to the module being loaded
  43. * opts.extensions - array of file extensions to search in order
  44. * opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search
  45. * opts.readFile - how to read files asynchronously
  46. * opts.isFile - function to asynchronously test whether a file exists
  47. * opts.isDirectory - function to asynchronously test whether a directory exists
  48. * opts.realpath - function to asynchronously resolve a potential symlink to its real path
  49. * `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the "main" field
  50. * pkg - package data
  51. * pkgfile - path to package.json
  52. * dir - directory for package.json
  53. * `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
  54. * pkg - package data
  55. * path - the path being resolved
  56. * relativePath - the path relative from the package.json location
  57. * returns - a relative path that will be joined from the package.json location
  58. * opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
  59. For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
  60. * request - the import specifier being resolved
  61. * start - lookup path
  62. * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
  63. * opts - the resolution options
  64. * `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)
  65. * request - the import specifier being resolved
  66. * start - lookup path
  67. * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
  68. * opts - the resolution options
  69. * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
  70. * opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
  71. This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
  72. **Note:** this property is currently `true` by default but it will be changed to
  73. `false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.
  74. default `opts` values:
  75. ```js
  76. {
  77. paths: [],
  78. basedir: __dirname,
  79. extensions: ['.js'],
  80. includeCoreModules: true,
  81. readFile: fs.readFile,
  82. isFile: function isFile(file, cb) {
  83. fs.stat(file, function (err, stat) {
  84. if (!err) {
  85. return cb(null, stat.isFile() || stat.isFIFO());
  86. }
  87. if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
  88. return cb(err);
  89. });
  90. },
  91. isDirectory: function isDirectory(dir, cb) {
  92. fs.stat(dir, function (err, stat) {
  93. if (!err) {
  94. return cb(null, stat.isDirectory());
  95. }
  96. if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
  97. return cb(err);
  98. });
  99. },
  100. realpath: function realpath(file, cb) {
  101. var realpath = typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath;
  102. realpath(file, function (realPathErr, realPath) {
  103. if (realPathErr && realPathErr.code !== 'ENOENT') cb(realPathErr);
  104. else cb(null, realPathErr ? file : realPath);
  105. });
  106. },
  107. moduleDirectory: 'node_modules',
  108. preserveSymlinks: true
  109. }
  110. ```
  111. ## resolve.sync(id, opts)
  112. Synchronously resolve the module path string `id`, returning the result and
  113. throwing an error when `id` can't be resolved.
  114. options are:
  115. * opts.basedir - directory to begin resolving from
  116. * opts.extensions - array of file extensions to search in order
  117. * opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search
  118. * opts.readFile - how to read files synchronously
  119. * opts.isFile - function to synchronously test whether a file exists
  120. * opts.isDirectory - function to synchronously test whether a directory exists
  121. * opts.realpathSync - function to synchronously resolve a potential symlink to its real path
  122. * `opts.packageFilter(pkg, dir)` - transform the parsed package.json contents before looking at the "main" field
  123. * pkg - package data
  124. * dir - directory for package.json (Note: the second argument will change to "pkgfile" in v2)
  125. * `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
  126. * pkg - package data
  127. * path - the path being resolved
  128. * relativePath - the path relative from the package.json location
  129. * returns - a relative path that will be joined from the package.json location
  130. * opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
  131. For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
  132. * request - the import specifier being resolved
  133. * start - lookup path
  134. * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
  135. * opts - the resolution options
  136. * `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)
  137. * request - the import specifier being resolved
  138. * start - lookup path
  139. * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
  140. * opts - the resolution options
  141. * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
  142. * opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
  143. This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
  144. **Note:** this property is currently `true` by default but it will be changed to
  145. `false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.
  146. default `opts` values:
  147. ```js
  148. {
  149. paths: [],
  150. basedir: __dirname,
  151. extensions: ['.js'],
  152. includeCoreModules: true,
  153. readFileSync: fs.readFileSync,
  154. isFile: function isFile(file) {
  155. try {
  156. var stat = fs.statSync(file);
  157. } catch (e) {
  158. if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
  159. throw e;
  160. }
  161. return stat.isFile() || stat.isFIFO();
  162. },
  163. isDirectory: function isDirectory(dir) {
  164. try {
  165. var stat = fs.statSync(dir);
  166. } catch (e) {
  167. if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
  168. throw e;
  169. }
  170. return stat.isDirectory();
  171. },
  172. realpathSync: function realpathSync(file) {
  173. try {
  174. var realpath = typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;
  175. return realpath(file);
  176. } catch (realPathErr) {
  177. if (realPathErr.code !== 'ENOENT') {
  178. throw realPathErr;
  179. }
  180. }
  181. return file;
  182. },
  183. moduleDirectory: 'node_modules',
  184. preserveSymlinks: true
  185. }
  186. ```
  187. # install
  188. With [npm](https://npmjs.org) do:
  189. ```sh
  190. npm install resolve
  191. ```
  192. # license
  193. MIT