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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

npm-which's Issues

npm-which only finds binaries in the top-level .bin

me@host:~/some_project/node_modules/lint-trap$ ./node_modules/.bin/npm-which jshint
/home/me/some_project/node_modules/lint-trap/node_modules/.bin/jshint
me@host:~/some_project/node_modules/lint-trap$ cd -
/home/me/code/node-uber-lint/test/fixtures/sirvice.git
me@host:~/some_project$ ./node_modules/lint-trap/node_modules/.bin/npm-which jshint
jshint not found
me@host:~/some_project$ 

Now, I think the above behavior is probably correct from the point of view of the CLI utility. The problem I have is that the same thing happens when npm-which is called programmatically in a submodule.

For my module, which is meant to always be installed as a top level dependency in a project, npm-which does not find the binaries in its own .bin folder.

CLI should error if not found

I'm trying to use the CLI to check for a installed package and then return an error if it's unavailable. For example:

npm-which prettier && echo "found" || echo "not found

Currently, this will always print found since the cli never posts an error message on failure.

Implicit dependency on npm

When using mhart/alpine-node:base-7.2.0 base image for docker container, that has only Node without npm this happens:

/usr/src/app/node_modules/npm-which/index.js:58
      if (err) throw err
               ^

Error: not found: npm
    at getNotFoundError (/usr/src/app/node_modules/which/which.js:13:12)
    at Function.whichSync [as sync] (/usr/src/app/node_modules/which/which.js:131:9)
    at whichNpm (/usr/src/app/node_modules/npm-path/index.js:150:23)
    at getPath (/usr/src/app/node_modules/npm-path/index.js:26:3)
    at Function.getPathSync [as getSync] (/usr/src/app/node_modules/npm-path/index.js:74:3)
    at Function.npmWhich.sync (/usr/src/app/node_modules/npm-which/index.js:50:29)
    at new exports.StatsdSubmit (/usr/src/app/node_modules/@packetloop/statsd-submit/lib/statsdSubmit.js:13:31)
    at Object.<anonymous> (/usr/src/app/node_modules/@packetloop/log/lib/requestLog.js:27:16)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)

If library depends on NPM then it should probably be in dependencies.

README update would be neat too

Cheers ๐Ÿป

Question: Will this find .cmd binaries on Windows?

I'm trying to figure out the best way of supporting Windows in a project I work on. When a user on Windows does a global install, it seems that npm creates a .cmd executable. However, that does not seem to occur when it installs dependencies into node_modules. Therefore, on Windows it seems to not be possible to spawn a binary of a module which my project depends on unless it is globally installed by the user.

So, if I've explained the situation clearly enough, can you say if using npm-which (or perhaps npm-run) would solve the issues I'm facing?

`process.env.PATH` doesn't get reset

If you're setting a custom env modifying the PATH it doesn't get reset after running. E.g:

which.sync('someModule', {env: {PATH: 'customPath/.bin'}});
console.log(process.env.PATH);
//=> only npm dirs

I don't think this is the expected behaviour.

Promise support

Would be nice if this library has built-in promise support, so that I can do await which('foo') without doing a promisification. Thanks for the library!

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.