# resolve-package-path

> a special purpose fast memoizing way to resolve a node modules package.json

Latest version **4.0.3** (published 2021-09-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install resolve-package-path
pnpm add resolve-package-path
yarn add resolve-package-path
bun add 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 | 4.0.3 |
| Published | 2021-09-13 |
| First published | 2019-02-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 12 |
| Dependencies | 1 |
| Unpacked size | 52.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 32 |
| Maintainers | rwjblue, stefanpenner |

## Links

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

## Dependencies (1)

- [path-root](https://npm.io/package/path-root.md) ^0.1.1

## Recent versions

- 4.0.3 (latest) — 2021-09-13
- 4.0.2 — 2021-09-13
- 4.0.1 — 2021-06-04
- 4.0.0 — 2021-05-10
- 3.1.0 — 2020-10-19
- 3.0.1 — 2020-10-13
- 3.0.0 — 2020-10-13
- 2.0.0 — 2019-12-09
- 1.2.7 — 2019-04-16
- 1.2.6 — 2019-03-26
- 1.2.5 — 2019-03-26
- 1.2.4 — 2019-03-07
- 1.2.3 — 2019-03-06
- 1.2.2 — 2019-03-05
- 1.2.1 — 2019-03-05
- … 14 more at https://npm.io/package/resolve-package-path/versions

## README

# resolve-package-path [![CI](https://github.com/stefanpenner/resolve-package-path/workflows/CI/badge.svg)](https://github.com/stefanpenner/resolve-package-path/actions/workflows/ci.yml)

This project is special-purpose, made to resolve `package.json` files for:

  - a given module name and basedir or
  - a given basedir

It cannot and does not resolve anything else.

To achieve its file-resolution performance, it does two specific things:

- It memoizes results identically to node's `require`. Specifically,
  for a given moduleName and baseDir it will, for the duration of the process,
  always return the exact same response.

- It re-implements the parts of `require.resolve` needed to resolve package.json
  files ONLY. This removes unneeded I/O. (based on @davecombs approach)

## Usage

```sh
yarn add resolve-package-path
```

```js
const resolvePackagePath = require('resolve-package-path');

resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from') // => /path/to/rsvp.json or null

const { findUpPackagePath } = resolvePackagePath;
findUpPackagePath('base-dir/to/start') // => path/to/package.json or null
```

## Advanced usage

### Preserve Symlinks

Node supports `--preserve-symlinks` and `NODE_PRESERVE_SYMLINKS=1` for compatibility this library respects these.

### Disable default caching

Although by default `resolve-package-path` caches or memoizes results, this feature can be disabled:

```js
const resolvePackagePath = require('resolve-package-path');

resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from', false) // => uncached result /path/to/rsvp.json or null

const { findUpPackagePath } = resolvePackagePath;
findUpPackagePath('base-dir/to/start', false) // => path/to/package.json or null
```

### Purge the cache

```js
const resolvePackagePath = require('resolve-package-path');
resolvePackagePath._resetCache();
```

### Provide an alternative cache

In some advanced circumtances, you may want to gain access to the cache to share between more systems.
In that case, a cache instance of the following form can be provided as a third argument:

```js
cache = {
  RESOLVED_PACKAGE_PATH: new Map(),
  REAL_FILE_PATH: new Map(),
  REAL_DIRECTORY_PATH: new Map(),
};
findUpCache = new Map();

const resolvePackagePath = require('resolve-package-path');
resolvePackagePath('rsvp', 'path/to/start/from', cache);

const { findUpPackagePath } = resolvePackagePath;
findUpPackagePath('base-dir/to/start', findUpCache) // => path/to/package.json or null
```

### Use internal helper functions

For consumers who also do `getRealFilePath` or
`getRealDirectoryPath` calls on relevant paths, we expose them as utilities.
These utilties ensure identical functionality to resolve-package-path, and a
shared cache, which may help reduce IO.

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