Blame view
node_modules/resolve/readme.markdown
3.63 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# resolve implements the [node `require.resolve()` algorithm](https://nodejs.org/api/modules.html#modules_all_together) such that you can `require.resolve()` on behalf of a file asynchronously and synchronously [](http://travis-ci.org/substack/node-resolve) # example asynchronously resolve: ``` js var resolve = require('resolve'); resolve('tap', { basedir: __dirname }, function (err, res) { if (err) console.error(err) else console.log(res) }); ``` ``` $ node example/async.js /home/substack/projects/node-resolve/node_modules/tap/lib/main.js ``` synchronously resolve: ``` js var resolve = require('resolve'); var res = resolve.sync('tap', { basedir: __dirname }); console.log(res); ``` ``` $ node example/sync.js /home/substack/projects/node-resolve/node_modules/tap/lib/main.js ``` # methods ``` js var resolve = require('resolve') ``` ## resolve(id, opts={}, cb) Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`. options are: * opts.basedir - directory to begin resolving from * opts.package - `package.json` data applicable to the module being loaded * opts.extensions - array of file extensions to search in order * opts.readFile - how to read files asynchronously * opts.isFile - function to asynchronously test whether a file exists * opts.packageFilter - transform the parsed package.json contents before looking at the "main" field * opts.pathFilter(pkg, path, relativePath) - transform a path within a package * pkg - package data * path - the path being resolved * relativePath - the path relative from the package.json location * returns - a relative path that will be joined from the package.json location * opts.paths - require.paths array to use if nothing is found on the normal node_modules recursive walk (probably don't use this) * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"` default `opts` values: ``` javascript { paths: [], basedir: __dirname, extensions: [ '.js' ], readFile: fs.readFile, isFile: function (file, cb) { fs.stat(file, function (err, stat) { if (err && err.code === 'ENOENT') cb(null, false) else if (err) cb(err) else cb(null, stat.isFile()) }); }, moduleDirectory: 'node_modules' } ``` ## resolve.sync(id, opts) Synchronously resolve the module path string `id`, returning the result and throwing an error when `id` can't be resolved. options are: * opts.basedir - directory to begin resolving from * opts.extensions - array of file extensions to search in order * opts.readFile - how to read files synchronously * opts.isFile - function to synchronously test whether a file exists * `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json * contents before looking at the "main" field * opts.paths - require.paths array to use if nothing is found on the normal node_modules recursive walk (probably don't use this) * opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"` default `opts` values: ``` javascript { paths: [], basedir: __dirname, extensions: [ '.js' ], readFileSync: fs.readFileSync, isFile: function (file) { try { return fs.statSync(file).isFile() } catch (e) { return false } }, moduleDirectory: 'node_modules' } ```` ## resolve.isCore(pkg) Return whether a package is in core. # install With [npm](https://npmjs.org) do: ``` npm install resolve ``` # license MIT |