# @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)

Latest version **1.0.7** (published 2023-09-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rigor789/resolve-package-path
pnpm add @rigor789/resolve-package-path
yarn add @rigor789/resolve-package-path
bun add @rigor789/resolve-package-path
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2023-09-11 |
| First published | 2021-09-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Igor Randjelovic |
| Maintainers | rigor789 |

## Links

- npm: https://www.npmjs.com/package/@rigor789/resolve-package-path
- Repository: https://github.com/rigor789/resolve-package-path
- Homepage: https://github.com/rigor789/resolve-package-path#readme
- Issues: https://github.com/rigor789/resolve-package-path/issues
- npm.io page: https://npm.io/package/@rigor789/resolve-package-path

## Recent versions

- 1.0.7 (latest) — 2023-09-11
- 1.0.6 — 2023-09-11
- 1.0.5 — 2021-09-27
- 1.0.4 — 2021-09-27
- 1.0.3 — 2021-09-27
- 1.0.2 — 2021-09-27
- 1.0.1 — 2021-09-27
- 1.0.0 — 2021-09-27

## 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("<packageName>/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("<packageName>/package.json")`
2. then falling back to `require.resolve("<packageName>")` and finding the last index of `node_modules/<packageName>`
3. finally falls back to the same `require.resolve("<packageName>")` 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

---
_Source: https://npm.io/package/@rigor789/resolve-package-path · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
