Giter Club home page Giter Club logo

npm-which's Introduction

npm-which

Locate a program or locally installed node module executable

Build Status

NPM NPM

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

> 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

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

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.
which('tape', {cwd: '/some/other/path'}, function() {
  // ...
})

Command Line

> 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

# 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

npm-which's People

Contributors

timoxley avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.