Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rigor789/resolve-package-path
https://github.com/rigor789/resolve-package-path
javascript node nodejs npm npm-package yarn
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/rigor789/resolve-package-path
- Owner: rigor789
- Created: 2021-09-27T13:29:03.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-11T07:31:17.000Z (over 1 year ago)
- Last Synced: 2024-12-17T22:39:37.011Z (6 days ago)
- Topics: javascript, node, nodejs, npm, npm-package, yarn
- Language: JavaScript
- Homepage:
- Size: 73.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @rigor789/resolve-package-path
[![CI](https://github.com/rigor789/resolve-package-path/actions/workflows/ci.yml/badge.svg)](https://github.com/rigor789/resolve-package-path/actions/workflows/ci.yml)
Small utility (no external dependencies) for finding the path of a package across different node versions.
---
```bash
npm i --save @rigor789/resolve-package-path
# or
yarn add @rigor789/resolve-package-path
# or
pnpm add @rigor789/resolve-package-path
```---
The reason this exists is because `require.resolve("/package.json")` can fail if a package defines `exports` in their `package.json` and does not include `package.json`.
Some details on the issue can be found here: https://github.com/nodejs/node/issues/33460
This package works around the issue by:
1. first trying a regular `require.resolve("/package.json")`
2. then falling back to `require.resolve("")` and finding the last index of `node_modules/`
3. finally falls back to the same `require.resolve("")` and a filesystem traversal upwards until a matching package.json is found
4. if the package is not found, returns `false`# API
## resolvePackagePath(packageName: string, options?)
Resolves the package path. Options accepts anything that `require.resovle("", options)` accepts. [See Node Docs](https://nodejs.org/api/modules.html#modules_require_resolve_request_options)
Example:
```js
const { resolvePackagePath } = require("@rigor789/resolve-package-path");console.log(resolvePackagePath('package-a')) // /path/to/package-a
console.log(resolvePackagePath('@scoped/package-a')) // /path/to/@scoped/package-a
```## resolvePackageJSONPath(packageName: string, options?)
Same as `resolvePackagePath` but returns the path to the `package.json` instead.
Example:
```js
const { resolvePackageJSONPath } = require("@rigor789/resolve-package-path");console.log(resolvePackageJSONPath('package-a')) // /path/to/package-a/package.json
console.log(resolvePackageJSONPath('@scoped/package-a')) // /path/to/@scoped/package-a/package.json
```# License
MIT