You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.markdown 8.3KB

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