{"id":13818688,"url":"https://github.com/browserify/resolve","last_synced_at":"2025-05-12T07:49:38.623Z","repository":{"id":519404,"uuid":"1915099","full_name":"browserify/resolve","owner":"browserify","description":"Implements the node.js require.resolve() algorithm","archived":false,"fork":false,"pushed_at":"2024-12-30T20:10:52.000Z","size":852,"stargazers_count":783,"open_issues_count":16,"forks_count":185,"subscribers_count":15,"default_branch":"main","last_synced_at":"2025-05-11T05:49:14.192Z","etag":null,"topics":["javascript","module","modules","node","nodejs","require","resolve","resolver"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/browserify.png","metadata":{"files":{"readme":"readme.markdown","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["ljharb"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":"npm/resolve","community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2011-06-18T09:37:24.000Z","updated_at":"2025-05-07T15:57:34.000Z","dependencies_parsed_at":"2025-02-14T12:15:31.676Z","dependency_job_id":null,"html_url":"https://github.com/browserify/resolve","commit_stats":{"total_commits":570,"total_committers":60,"mean_commits":9.5,"dds":"0.37368421052631584","last_synced_commit":"fc6c18388011da55117fda59759275a4fb94d8ae"},"previous_names":["substack/node-resolve","browserify/node-resolve"],"tags_count":136,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserify%2Fresolve","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserify%2Fresolve/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserify%2Fresolve/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserify%2Fresolve/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/browserify","download_url":"https://codeload.github.com/browserify/resolve/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253523732,"owners_count":21921818,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","module","modules","node","nodejs","require","resolve","resolver"],"created_at":"2024-08-04T08:00:22.255Z","updated_at":"2025-05-11T05:49:19.144Z","avatar_url":"https://github.com/browserify.png","language":"JavaScript","readme":"# resolve \u003csup\u003e[![Version Badge][2]][1]\u003c/sup\u003e\n\nimplements 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\n\n[![github actions][actions-image]][actions-url]\n[![coverage][codecov-image]][codecov-url]\n[![dependency status][5]][6]\n[![dev dependency status][7]][8]\n[![License][license-image]][license-url]\n[![Downloads][downloads-image]][downloads-url]\n\n[![npm badge][11]][1]\n\n# example\n\nasynchronously resolve:\n\n```js\nvar resolve = require('resolve/async'); // or, require('resolve')\nresolve('tap', { basedir: __dirname }, function (err, res) {\n    if (err) console.error(err);\n    else console.log(res);\n});\n```\n\n```\n$ node example/async.js\n/home/substack/projects/node-resolve/node_modules/tap/lib/main.js\n```\n\nsynchronously resolve:\n\n```js\nvar resolve = require('resolve/sync'); // or, `require('resolve').sync\nvar res = resolve('tap', { basedir: __dirname });\nconsole.log(res);\n```\n\n```\n$ node example/sync.js\n/home/substack/projects/node-resolve/node_modules/tap/lib/main.js\n```\n\n# methods\n\n```js\nvar resolve = require('resolve');\nvar async = require('resolve/async');\nvar sync = require('resolve/sync');\n```\n\nFor both the synchronous and asynchronous methods, errors may have any of the following `err.code` values:\n\n- `MODULE_NOT_FOUND`: the given path string (`id`) could not be resolved to a module\n- `INVALID_BASEDIR`: the specified `opts.basedir` doesn't exist, or is not a directory\n- `INVALID_PACKAGE_MAIN`: a `package.json` was encountered with an invalid `main` property (eg. not a string)\n\n## resolve(id, opts={}, cb)\n\nAsynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.\n\noptions are:\n\n* opts.basedir - directory to begin resolving from\n\n* opts.package - `package.json` data applicable to the module being loaded\n\n* opts.extensions - array of file extensions to search in order\n\n* opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search\n\n* opts.readFile - how to read files asynchronously\n\n* opts.isFile - function to asynchronously test whether a file exists\n\n* opts.isDirectory - function to asynchronously test whether a file exists and is a directory\n\n* opts.realpath - function to asynchronously resolve a potential symlink to its real path\n\n* `opts.readPackage(readFile, pkgfile, cb)` - function to asynchronously read and parse a package.json file\n  * readFile - the passed `opts.readFile` or `fs.readFile` if not specified\n  * pkgfile - path to package.json\n  * cb - callback. a SyntaxError error argument will be ignored, all other error arguments will be treated as an error.\n\n* `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the \"main\" field\n  * pkg - package data\n  * pkgfile - path to package.json\n  * dir - directory that contains package.json\n\n* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package\n  * pkg - package data\n  * path - the path being resolved\n  * relativePath - the path relative from the package.json location\n  * returns - a relative path that will be joined from the package.json location\n\n* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)\n\n  For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function\n    * request - the import specifier being resolved\n    * start - lookup path\n    * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution\n    * opts - the resolution options\n\n* `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)\n    * request - the import specifier being resolved\n    * start - lookup path\n    * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution\n    * opts - the resolution options\n\n* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `\"node_modules\"`\n\n* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.\nThis is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.\n\ndefault `opts` values:\n\n```js\n{\n    paths: [],\n    basedir: __dirname,\n    extensions: ['.js'],\n    includeCoreModules: true,\n    readFile: fs.readFile,\n    isFile: function isFile(file, cb) {\n        fs.stat(file, function (err, stat) {\n            if (!err) {\n                return cb(null, stat.isFile() || stat.isFIFO());\n            }\n            if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);\n            return cb(err);\n        });\n    },\n    isDirectory: function isDirectory(dir, cb) {\n        fs.stat(dir, function (err, stat) {\n            if (!err) {\n                return cb(null, stat.isDirectory());\n            }\n            if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);\n            return cb(err);\n        });\n    },\n    realpath: function realpath(file, cb) {\n        var realpath = typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath;\n        realpath(file, function (realPathErr, realPath) {\n            if (realPathErr \u0026\u0026 realPathErr.code !== 'ENOENT') cb(realPathErr);\n            else cb(null, realPathErr ? file : realPath);\n        });\n    },\n    readPackage: function defaultReadPackage(readFile, pkgfile, cb) {\n        readFile(pkgfile, function (readFileErr, body) {\n            if (readFileErr) cb(readFileErr);\n            else {\n                try {\n                    var pkg = JSON.parse(body);\n                    cb(null, pkg);\n                } catch (jsonErr) {\n                    cb(jsonErr);\n                }\n            }\n        });\n    },\n    moduleDirectory: 'node_modules',\n    preserveSymlinks: false\n}\n```\n\n## resolve.sync(id, opts)\n\nSynchronously resolve the module path string `id`, returning the result and\nthrowing an error when `id` can't be resolved.\n\noptions are:\n\n* opts.basedir - directory to begin resolving from\n\n* opts.extensions - array of file extensions to search in order\n\n* opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search\n\n* opts.readFileSync - how to read files synchronously\n\n* opts.isFile - function to synchronously test whether a file exists\n\n* opts.isDirectory - function to synchronously test whether a file exists and is a directory\n\n* opts.realpathSync - function to synchronously resolve a potential symlink to its real path\n\n* `opts.readPackageSync(readFileSync, pkgfile)` - function to synchronously read and parse a package.json file. a thrown SyntaxError will be ignored, all other exceptions will propagate.\n  * readFileSync - the passed `opts.readFileSync` or `fs.readFileSync` if not specified\n  * pkgfile - path to package.json\n\n* `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the \"main\" field\n  * pkg - package data\n  * pkgfile - path to package.json\n  * dir - directory that contains package.json\n\n* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package\n  * pkg - package data\n  * path - the path being resolved\n  * relativePath - the path relative from the package.json location\n  * returns - a relative path that will be joined from the package.json location\n\n* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)\n\n  For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function\n    * request - the import specifier being resolved\n    * start - lookup path\n    * getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution\n    * opts - the resolution options\n\n* `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)\n    * request - the import specifier being resolved\n    * start - lookup path\n    * getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution\n    * opts - the resolution options\n\n* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `\"node_modules\"`\n\n* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.\nThis is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.\n\ndefault `opts` values:\n\n```js\n{\n    paths: [],\n    basedir: __dirname,\n    extensions: ['.js'],\n    includeCoreModules: true,\n    readFileSync: fs.readFileSync,\n    isFile: function isFile(file) {\n        try {\n            var stat = fs.statSync(file);\n        } catch (e) {\n            if (e \u0026\u0026 (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;\n            throw e;\n        }\n        return stat.isFile() || stat.isFIFO();\n    },\n    isDirectory: function isDirectory(dir) {\n        try {\n            var stat = fs.statSync(dir);\n        } catch (e) {\n            if (e \u0026\u0026 (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;\n            throw e;\n        }\n        return stat.isDirectory();\n    },\n    realpathSync: function realpathSync(file) {\n        try {\n            var realpath = typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;\n            return realpath(file);\n        } catch (realPathErr) {\n            if (realPathErr.code !== 'ENOENT') {\n                throw realPathErr;\n            }\n        }\n        return file;\n    },\n    readPackageSync: function defaultReadPackageSync(readFileSync, pkgfile) {\n        return JSON.parse(readFileSync(pkgfile));\n    },\n    moduleDirectory: 'node_modules',\n    preserveSymlinks: false\n}\n```\n\n# install\n\nWith [npm](https://npmjs.org) do:\n\n```sh\nnpm install resolve\n```\n\n# license\n\nMIT\n\n[1]: https://npmjs.org/package/resolve\n[2]: https://versionbadg.es/browserify/resolve.svg\n[5]: https://david-dm.org/browserify/resolve.svg\n[6]: https://david-dm.org/browserify/resolve\n[7]: https://david-dm.org/browserify/resolve/dev-status.svg\n[8]: https://david-dm.org/browserify/resolve#info=devDependencies\n[11]: https://nodei.co/npm/resolve.png?downloads=true\u0026stars=true\n[license-image]: https://img.shields.io/npm/l/resolve.svg\n[license-url]: LICENSE\n[downloads-image]: https://img.shields.io/npm/dm/resolve.svg\n[downloads-url]: https://npm-stat.com/charts.html?package=resolve\n[codecov-image]: https://codecov.io/gh/browserify/resolve/branch/main/graphs/badge.svg\n[codecov-url]: https://app.codecov.io/gh/browserify/resolve/\n[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/browserify/resolve\n[actions-url]: https://github.com/browserify/resolve/actions\n","funding_links":["https://github.com/sponsors/ljharb","https://tidelift.com/funding/github/npm/resolve"],"categories":["GIT 仓库"],"sub_categories":["其他"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrowserify%2Fresolve","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrowserify%2Fresolve","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrowserify%2Fresolve/lists"}