Git Product home page Git Product logo

Comments (1)

isaacs avatar isaacs commented on June 30, 2024 2

which only finds executables that are in the PATH. When running npm tests, the node_modules/.bin folder is put in the PATH, so that which can find them. Here's an example:

$ cat package.json
{
  "name": "which-node-modules-bin",
  "version": "1.0.0",
  "scripts": {
    "test": "node test.js",
    "path": "echo $PATH"
  },
  "dependencies": {
    "istanbul": "^0.4.5",
    "which": "^1.2.12"
  }
}

$ cat test.js
const which = require('which');
var resolved = which.sync('istanbul');

console.log(resolved);

$ npm test

> [email protected] test /Users/isaacs/dev/js/x/which-node-modules-bin
> node test.js

/Users/isaacs/dev/js/x/which-node-modules-bin/node_modules/.bin/istanbul

$ node test.js
/Users/isaacs/dev/js/x/which-node-modules-bin/node_modules/which/which.js:131
  throw getNotFoundError(cmd)
  ^

Error: not found: istanbul
    at getNotFoundError (/Users/isaacs/dev/js/x/which-node-modules-bin/node_modules/which/which.js:13:12)
    at Function.whichSync [as sync] (/Users/isaacs/dev/js/x/which-node-modules-bin/node_modules/which/which.js:131:9)
    at Object.<anonymous> (/Users/isaacs/dev/js/x/which-node-modules-bin/test.js:2:22)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)

$ npm run path

> [email protected] path /Users/isaacs/dev/js/x/which-node-modules-bin
> echo $PATH

/Users/isaacs/dev/npm/npm/bin/node-gyp-bin:/Users/isaacs/dev/js/x/which-node-modules-bin/node_modules/.bin:/Users/isaacs/.basher/cellar/bin:/Users/isaacs/bin:/Users/isaacs/.rvm/bin:/Users/isaacs/.basher/bin:/Users/isaacs/bin:/usr/local/bin:/usr/local/sbin:/usr/local/include:/usr/bin:/usr/sbin:/usr/libexec:/bin:/sbin

$ echo $PATH
/Users/isaacs/.basher/cellar/bin:/Users/isaacs/bin:/Users/isaacs/.rvm/bin:/Users/isaacs/.basher/bin:/Users/isaacs/bin:/usr/local/bin:/usr/local/sbin:/usr/local/include:/usr/bin:/usr/sbin:/usr/libexec:/bin:/sbin

Note that the non-npm PATH envronment variable does not lead to any istanbuls, so which can't find it.

Working as designed.

from node-which.

Related Issues (20)

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.