CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
readme.markdown301 linesDownload Raw Back to resolve
1# resolve <sup>[![Version Badge][2]][1]</sup>2 3implements 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 synchronously4 5[![github actions][actions-image]][actions-url]6[![coverage][codecov-image]][codecov-url]7[![License][license-image]][license-url]8[![Downloads][downloads-image]][downloads-url]9[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/10759/badge)](https://bestpractices.coreinfrastructure.org/projects/10759)10 11[![npm badge][11]][1]12 13# example14 15asynchronously resolve:16 17```js18var resolve = require('resolve/async'); // or, require('resolve')19resolve('tap', { basedir: __dirname }, function (err, res) {20    if (err) console.error(err);21    else console.log(res);22});23```24 25```26$ node example/async.js27/home/substack/projects/node-resolve/node_modules/tap/lib/main.js28```29 30synchronously resolve:31 32```js33var resolve = require('resolve/sync'); // or, `require('resolve').sync34var res = resolve('tap', { basedir: __dirname });35console.log(res);36```37 38```39$ node example/sync.js40/home/substack/projects/node-resolve/node_modules/tap/lib/main.js41```42 43# methods44 45```js46var resolve = require('resolve');47var async = require('resolve/async');48var sync = require('resolve/sync');49```50 51For both the synchronous and asynchronous methods, errors may have any of the following `err.code` values:52 53- `MODULE_NOT_FOUND`: the given path string (`id`) could not be resolved to a module54- `INVALID_BASEDIR`: the specified `opts.basedir` doesn't exist, or is not a directory55- `INVALID_PACKAGE_MAIN`: a `package.json` was encountered with an invalid `main` property (eg. not a string)56 57## resolve(id, opts={}, cb)58 59Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.60 61options are:62 63* opts.basedir - directory to begin resolving from64 65* opts.package - `package.json` data applicable to the module being loaded66 67* opts.extensions - array of file extensions to search in order68 69* opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search70 71* opts.readFile - how to read files asynchronously72 73* opts.isFile - function to asynchronously test whether a file exists74 75* opts.isDirectory - function to asynchronously test whether a file exists and is a directory76 77* opts.realpath - function to asynchronously resolve a potential symlink to its real path78 79* `opts.readPackage(readFile, pkgfile, cb)` - function to asynchronously read and parse a package.json file80  * readFile - the passed `opts.readFile` or `fs.readFile` if not specified81  * pkgfile - path to package.json82  * cb - callback83 84* `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the "main" field85  * pkg - package data86  * pkgfile - path to package.json87  * dir - directory that contains package.json88 89* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package90  * pkg - package data91  * path - the path being resolved92  * relativePath - the path relative from the package.json location93  * returns - a relative path that will be joined from the package.json location94 95* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)96 97  For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function98    * request - the import specifier being resolved99    * start - lookup path100    * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution101    * opts - the resolution options102 103* `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)104    * request - the import specifier being resolved105    * start - lookup path106    * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution107    * opts - the resolution options108 109* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`110 111* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.112This 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 to114`false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.115 116default `opts` values:117 118```js119{120    paths: [],121    basedir: __dirname,122    extensions: ['.js'],123    includeCoreModules: true,124    readFile: fs.readFile,125    isFile: function isFile(file, cb) {126        fs.stat(file, function (err, stat) {127            if (!err) {128                return cb(null, stat.isFile() || stat.isFIFO());129            }130            if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);131            return cb(err);132        });133    },134    isDirectory: function isDirectory(dir, cb) {135        fs.stat(dir, function (err, stat) {136            if (!err) {137                return cb(null, stat.isDirectory());138            }139            if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);140            return cb(err);141        });142    },143    realpath: function realpath(file, cb) {144        var realpath = typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath;145        realpath(file, function (realPathErr, realPath) {146            if (realPathErr && realPathErr.code !== 'ENOENT') cb(realPathErr);147            else cb(null, realPathErr ? file : realPath);148        });149    },150    readPackage: function defaultReadPackage(readFile, pkgfile, cb) {151        readFile(pkgfile, function (readFileErr, body) {152            if (readFileErr) cb(readFileErr);153            else {154                try {155                    var pkg = JSON.parse(body);156                    cb(null, pkg);157                } catch (jsonErr) {158                    cb(null);159                }160            }161        });162    },163    moduleDirectory: 'node_modules',164    preserveSymlinks: true165}166```167 168## resolve.sync(id, opts)169 170Synchronously resolve the module path string `id`, returning the result and171throwing an error when `id` can't be resolved.172 173options are:174 175* opts.basedir - directory to begin resolving from176 177* opts.extensions - array of file extensions to search in order178 179* opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search180 181* opts.readFileSync - how to read files synchronously182 183* opts.isFile - function to synchronously test whether a file exists184 185* opts.isDirectory - function to synchronously test whether a file exists and is a directory186 187* opts.realpathSync - function to synchronously resolve a potential symlink to its real path188 189* `opts.readPackageSync(readFileSync, pkgfile)` - function to synchronously read and parse a package.json file190  * readFileSync - the passed `opts.readFileSync` or `fs.readFileSync` if not specified191  * pkgfile - path to package.json192 193* `opts.packageFilter(pkg, dir)` - transform the parsed package.json contents before looking at the "main" field194  * pkg - package data195  * dir - directory that contains package.json (Note: the second argument will change to "pkgfile" in v2)196 197* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package198  * pkg - package data199  * path - the path being resolved200  * relativePath - the path relative from the package.json location201  * returns - a relative path that will be joined from the package.json location202 203* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)204 205  For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function206    * request - the import specifier being resolved207    * start - lookup path208    * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution209    * opts - the resolution options210 211* `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)212    * request - the import specifier being resolved213    * start - lookup path214    * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution215    * opts - the resolution options216 217* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`218 219* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.220This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.221**Note:** this property is currently `true` by default but it will be changed to222`false` in the next major version because *Node's resolution algorithm does not preserve symlinks by default*.223 224default `opts` values:225 226```js227{228    paths: [],229    basedir: __dirname,230    extensions: ['.js'],231    includeCoreModules: true,232    readFileSync: fs.readFileSync,233    isFile: function isFile(file) {234        try {235            var stat = fs.statSync(file);236        } catch (e) {237            if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;238            throw e;239        }240        return stat.isFile() || stat.isFIFO();241    },242    isDirectory: function isDirectory(dir) {243        try {244            var stat = fs.statSync(dir);245        } catch (e) {246            if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;247            throw e;248        }249        return stat.isDirectory();250    },251    realpathSync: function realpathSync(file) {252        try {253            var realpath = typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;254            return realpath(file);255        } catch (realPathErr) {256            if (realPathErr.code !== 'ENOENT') {257                throw realPathErr;258            }259        }260        return file;261    },262    readPackageSync: function defaultReadPackageSync(readFileSync, pkgfile) {263        var body = readFileSync(pkgfile);264        try {265            var pkg = JSON.parse(body);266            return pkg;267        } catch (jsonErr) {}268    },269    moduleDirectory: 'node_modules',270    preserveSymlinks: true271}272```273 274# install275 276With [npm](https://npmjs.org) do:277 278```sh279npm install resolve280```281 282# license283 284MIT285 286[1]: https://npmjs.org/package/resolve287[2]: https://versionbadg.es/browserify/resolve.svg288[5]: https://david-dm.org/browserify/resolve.svg289[6]: https://david-dm.org/browserify/resolve290[7]: https://david-dm.org/browserify/resolve/dev-status.svg291[8]: https://david-dm.org/browserify/resolve#info=devDependencies292[11]: https://nodei.co/npm/resolve.png?downloads=true&stars=true293[license-image]: https://img.shields.io/npm/l/resolve.svg294[license-url]: LICENSE295[downloads-image]: https://img.shields.io/npm/dm/resolve.svg296[downloads-url]: https://npm-stat.com/charts.html?package=resolve297[codecov-image]: https://codecov.io/gh/browserify/resolve/branch/main/graphs/badge.svg298[codecov-url]: https://app.codecov.io/gh/browserify/resolve/299[actions-image]: https://img.shields.io/github/check-runs/browserify/resolve/main300[actions-url]: https://github.com/browserify/resolve/actions301