Kaido Kert | f585e26 | 2020-06-08 11:42:28 -0700 | [diff] [blame] | 1 | # resolve |
| 2 | |
| 3 | implements the [node `require.resolve()` |
| 4 | algorithm](http://nodejs.org/docs/v0.4.8/api/all.html#all_Together...) |
| 5 | such that you can `require.resolve()` on behalf of a file asynchronously and |
| 6 | synchronously |
| 7 | |
| 8 | [](http://travis-ci.org/substack/node-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.packageFilter - transform the parsed package.json contents before looking |
| 63 | at the "main" field |
| 64 | |
| 65 | * opts.pathFilter(pkg, path, relativePath) - transform a path within a package |
| 66 | * pkg - package data |
| 67 | * path - the path being resolved |
| 68 | * relativePath - the path relative from the package.json location |
| 69 | * returns - a relative path that will be joined from the package.json location |
| 70 | |
| 71 | * opts.paths - require.paths array to use if nothing is found on the normal |
| 72 | node_modules recursive walk (probably don't use this) |
| 73 | |
| 74 | * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"` |
| 75 | |
| 76 | default `opts` values: |
| 77 | |
| 78 | ``` javascript |
| 79 | { |
| 80 | paths: [], |
| 81 | basedir: __dirname, |
| 82 | extensions: [ '.js' ], |
| 83 | readFile: fs.readFile, |
| 84 | isFile: function (file, cb) { |
| 85 | fs.stat(file, function (err, stat) { |
| 86 | if (err && err.code === 'ENOENT') cb(null, false) |
| 87 | else if (err) cb(err) |
| 88 | else cb(null, stat.isFile()) |
| 89 | }); |
| 90 | }, |
| 91 | moduleDirectory: 'node_modules' |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | ## resolve.sync(id, opts) |
| 96 | |
| 97 | Synchronously resolve the module path string `id`, returning the result and |
| 98 | throwing an error when `id` can't be resolved. |
| 99 | |
| 100 | options are: |
| 101 | |
| 102 | * opts.basedir - directory to begin resolving from |
| 103 | |
| 104 | * opts.extensions - array of file extensions to search in order |
| 105 | |
| 106 | * opts.readFile - how to read files synchronously |
| 107 | |
| 108 | * opts.isFile - function to synchronously test whether a file exists |
| 109 | |
| 110 | * `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json |
| 111 | * contents before looking at the "main" field |
| 112 | |
| 113 | * opts.paths - require.paths array to use if nothing is found on the normal |
| 114 | node_modules recursive walk (probably don't use this) |
| 115 | |
| 116 | * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"` |
| 117 | |
| 118 | default `opts` values: |
| 119 | |
| 120 | ``` javascript |
| 121 | { |
| 122 | paths: [], |
| 123 | basedir: __dirname, |
| 124 | extensions: [ '.js' ], |
| 125 | readFileSync: fs.readFileSync, |
| 126 | isFile: function (file) { |
| 127 | try { return fs.statSync(file).isFile() } |
| 128 | catch (e) { return false } |
| 129 | }, |
| 130 | moduleDirectory: 'node_modules' |
| 131 | } |
| 132 | ```` |
| 133 | |
| 134 | ## resolve.isCore(pkg) |
| 135 | |
| 136 | Return whether a package is in core. |
| 137 | |
| 138 | # install |
| 139 | |
| 140 | With [npm](https://npmjs.org) do: |
| 141 | |
| 142 | ``` |
| 143 | npm install resolve |
| 144 | ``` |
| 145 | |
| 146 | # license |
| 147 | |
| 148 | MIT |