npm / node-which

Like which(1) unix command. Find the first instance of an executable in the PATH.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

which sync cannot work on windows

young-mu opened this issue · comments

commented

In Git Bash on Windows 10

$ which esptool.py
/c/Python27/Scripts/esptool.py

c:/Python27/Scripts is also added in PATH ENV.

But, the following code using which module is failed.

var which = require('which');
var cmd = 'esptool.py';

try {
    var ret = which.sync(cmd);
} catch (error) {
    console.log('error', error);
}

console.log(ret);
$ node test.js
error { Error: not found: esptool.py
    at getNotFoundError (C:\Users\Young\Desktop\test\node_modules\which\which.js:13:12)
    at Function.whichSync [as sync] (C:\Users\Young\Desktop\test\node_modules\which\which.js:132:9)
    at Object.<anonymous> (C:\Users\Young\Desktop\test\test.js:6:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7) code: 'ENOENT' }
undefined

What's wrong ???

My guess is that you don't have .py in your PATHEXT environment variable. Therefor, even though esptool.py might be in the PATH, and have a mode including +x, it isn't executable because Windows systems judge executability based on the path extension. Note that if you ran esptool.py in the cmd.exe terminal, it'll say that it doesn't recognize the command.

bash on windows does not judge executability this way, since it's a ported unix tool that follows unix conventions in most things.

Put .py in your PATHEXT environment variable, and it'll work.

Or you can pass a modified pathExt by doing which.sync('esptool.py', {pathExt: ';' + process.env.PATHEXT}). That works because PATHEXT is colon-delimited, and if there's an entry at the start of it that is '', then it'll allow any command that is fully specified.

I know this is all a bit bonkers if you're more familiar with the Unix way, but it's how Windows works, and the point of this lib is to work out if exec(command) will find a thing or not, and which one it'll find.