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.

202 lines
6.3 KiB

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