# npm-which

> Locate a program or locally installed node module's executable

Latest version **3.0.1** (published 2016-07-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install npm-which
pnpm add npm-which
yarn add npm-which
bun add npm-which
```

Provides the command `npm-which`.

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2016-07-22 |
| First published | 2014-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/npm-which) |
| Module format | CommonJS |
| Node | >=4.2.0 |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 77 |
| Author | Tim Oxley |
| Maintainers | timoxley |
| Keywords | npm, path, executable, run |

## Links

- npm: https://www.npmjs.com/package/npm-which
- Repository: https://github.com/timoxley/npm-which
- Issues: https://github.com/timoxley/npm-which/issues
- npm.io page: https://npm.io/package/npm-which

## Dependencies (3)

- [which](https://npm.io/package/which.md) ^1.2.10
- [npm-path](https://npm.io/package/npm-path.md) ^2.0.2
- [commander](https://npm.io/package/commander.md) ^2.9.0

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 3.0.1 (latest) — 2016-07-22
- 3.0.0 — 2016-07-22
- 2.0.0 — 2014-11-26
- 1.0.2 — 2014-10-28
- 1.0.1 — 2014-06-23
- 1.0.0 — 2014-04-21

## README

# npm-which

### Locate a program or locally installed node module executable

[![Build Status](https://travis-ci.org/timoxley/npm-which.svg?branch=master)](https://travis-ci.org/timoxley/npm-which)

[![NPM](https://nodei.co/npm/npm-which.png?downloads=true&downloadRank=true)](https://nodei.co/npm-dl/npm-which/)
[![NPM](https://nodei.co/npm-dl/npm-which.png?months=3&height=3&chrome)](https://nodei.co/npm/npm-which/)

Use `npm-which` to locate executables which may be installed in the
local 'node_modules/.bin', or in a parent 'node_modules/.bin' directory.

`npm-which` runs in the context of an npm lifecycle script with its npm-modified PATH.

i.e. if you install a module that has an executable script using npm install, that module's executable will be picked up by `npm-which` from anywhere in the ./node_modules tree.

## Installation

```bash
> npm install -g npm-which
```

## Usage

### Programmatic

`npm-which` will find executables relative to the cwd you supply.
The cwd is required in order to be explicit and reduce confusion when
things that should be found are not.

#### Asynchronous

```js
var which = require('npm-which')(process.cwd()) // remember to supply cwd
which('tape', function(err, pathToTape) {
  if (err) return console.error(err.message)
  console.log(pathToTape) // /Users/.../node_modules/.bin/tape
})
```

#### Synchronous

```js
var which = require('npm-which')(__dirname) // __dirname often good enough
var pathToTape = which.sync('tape')
console.log(pathToTape) // /Users/.../node_modules/.bin/tape
```

#### Options

Both async and sync versions take an optional options object:

* Set `options.env` if you wish to use something other than `process.env` (the default)
* Set `options.cwd` to supply the cwd as a named argument. Mainly for semi-backwards compatibility with npm-which 1.0.0.

```js
which('tape', {cwd: '/some/other/path'}, function() {
  // ...
})
```

### Command Line

```bash
> npm-which tape
/Users/timoxley/Projects/npm-which/node_modules/.bin/tape
```

This is the equivalent of running an npm script with the body: `which tape`.

### Example

```bash
# unless something is installed in a node_modules
# npm-which and which(1) will have the same output:

> which tape
/usr/local/bin/tape

> npm-which tape
/usr/local/bin/tape

# install tape local to current dir
# tape includes an executable 'tape'
> npm install tape
> ./node_modules/.bin/tape && echo 'found'
found

# vanilla which(1) still finds global tape
> which tape
/usr/local/bin/tape

# npm-which finds locally installed tape :)
> npm-which tape
/Users/timoxley/Projects/npm-which/node_modules/.bin/tape
```

## Why

#### npm is slow to boot

* Shelling out to `npm bin` is very slow; it has to wait for all of npm to boot up – this often takes longer than the actual script you want to execute!

#### Hard-coding paths to modules is very fragile

* You can't rely on './node_modules' actually containing your module! The module may exist much higher in the directory hierarchy.
* `npm bin` returns the location of the `./node_modules/.bin` directory, but it does not take into account being called within the context of another module, also, npm slow.
* If the module does exist in a parent directory, then './node_modules/.bin' will be missing your module's executable.

## License

MIT

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