fails to capture executable in local node_modules dir (?)
ORESoftware opened this issue · comments
Alexander Mills commented
I have this:
foo/
test.js
package.json
node_modules/
.bin/
istanbul
in test.js I have:
const which = require('which');
var resolved = which.sync('istanbul');
console.log(resolved);
in package.json I have:
"dependencies": {
"istanbul": "^0.4.5",
"which": "^1.2.12"
},
I get this error in test.js
Error: not found: istanbul
at getNotFoundError (/home/olegzandr/Documents/WebstormProjects/suman-test-projects/subprojects/node-path-test/node_modules/which/which.js:13:12)
at Function.whichSync [as sync] (/home/olegzandr/Documents/WebstormProjects/suman-test-projects/subprojects/node-path-test/node_modules/which/which.js:131:9)
at Object.<anonymous> (/home/olegzandr/Documents/WebstormProjects/suman-test-projects/subprojects/node-path-test/test.js:7:22)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
this seems to suggest that node-which does not pick up executables in ./node_modules/.bin
, ist das so?
isaacs commented
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
> which-node-modules-bin@1.0.0 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
> which-node-modules-bin@1.0.0 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.