Node - check existence of command in path

command-exists
nodejs
node child_process
nodejs exec command
nodejs which command
node js check if program is installed
electron exec
zsh check if command exists

I have a unique problem where I need to use Node to iterate over several Unix-style commands and see if they exist in the path of a Windows installation.

For instance, Windows doesn't support ls natively. However, supposing someone installed git and checked to include Unix commands, it would.

I need to know whether or not ls and other commands are in the system path.

Right now, I'm using child_process to run help on each command. I then check the response of running it. This is messy and dangerous. I don't want to run 30 arbitrary commands from Node:

var spawnSync = require('child_process').spawnSync;
var out = spawnSync('ls', ['/?'], {encoding: 'utf8'});

How else can I check for the existence of these commands?

You could use whereis in Linux, and where in Windows, to see if the executable can be found

var isWin = require('os').platform().indexOf('win') > -1;

var where = isWin ? 'where' : 'whereis';

var spawn = require('child_process').spawn;

var out = spawn(where + ' ls', ['/?'], {encoding: 'utf8'});

out.on('close', function (code) {
    console.log('exit code : ' + code);
});

Node - check existence of command in path - node.js - html, I have a unique problem where I need to use Node to iterate over several Unix- style commands and see if they exist in the path of a Windows installation. The way to check if a file exists in the filesystem, using Node.js, is by using the fs.existsSync () method: const fs = require('fs') const path = './file.txt' try { if (fs.existsSync(path)) { //file exists } } catch(err) { console.error(err) } This method is synchronous. This means that it’s blocking.

How to check if a file exists in Node.js, The way to check if a file exists in the filesystem, using Node.js, is by using the fs. existsSync() method: const fs = require('fs') const path = '. PRVG-04001: Failed to check existence of Windows registry key "{0}" on node "{1}", [{2}] Cause: Could not check the existence of specified Windows registry key on the identified node. Action: Look at the accompanying messages and respond accordingly. PRVG-04002: Missing '-user <user_name>' argument for selected privilege delegation method "{0}".

Could you not check in your Path directories, and see if the ls command file exists.

If the files doesn't exist in the directories, then it's not in path.

mathisonian/command-exists: node module to check if a , var commandExistsSync = require('command-exists').sync; // returns true/false; doesn't throw if (commandExistsSync(� -e FILE - True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).-f FILE - True if the FILE exists and is a regular file (not a directory or device).-G FILE - True if the FILE exists and has the same group as the user running the command.-h FILE - True if the FILE exists and is a symbolic link.

DON'T use child_process just for that, neither packages which use child_process internally. As Matt already answered, the most straightforward way is just checking under your Path.

Here is a NPM lookpath package which scans your $PATH or $Path.

const { lookpath } = require('lookpath');

const p = await lookpath('bash');
// "/bin/bash"

This is a Node.js port of exec.LookPath from Go.

Hope it helps

Node.js: Check if a command exists � GitHub, Node.js: Check if a command exists. cmd-exists.js. // returns a Promise which fulfills with the result of a shell command. // rejects with stderr. This command is used to list and display the children of a znode. Syntax ls /path Sample ls /MyFirstZnode Output [zk: localhost:2181(CONNECTED) 2] ls /MyFirstZnode [mysecondsubnode, myfirstsubnode] Check Status. Status describes the metadata of a specified znode. It contains details such as Timestamp, Version number, ACL, Data length, and Children znode.

Check if a Directory Exists in NodeJS, access() function. This function takes a directory path as an argument and is usually used to test a user's permission for a file or directory. But, if� XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps. The XML Example Document.

How to check if a file exists in Node.js, path — The file or directory path for testing the user's permissions. mode — An optional integer that specifies the accessibility check to be� Install npm install lookpath Background. I DON'T want to spawn child_process JUST in order to kick which, where, whereis, or command -v.. node.js - Node - check existence of command in path - Stack Overflow

javascript check if file exists in folder Code Example, Get code examples like "javascript check if file exists in folder" instantly right from your <b>Enter File Path: </b> function checkFileExist() { check if file exist sync js � how to know if a path exist nodejs � javascript electron� node module to check if a command-line command exists - mathisonian/command-exists

Comments
  • There's actually middleware for that ?
  • That's sloppy, lazy middleware. It just does a var child = exec('which ' + commandName);. The command which doesn't exist on Windows. I already looked for a cross-platform shell command to determine process existence, and I can't find one.
  • Not sure if there's something magical that works in everything, you'd probably be better of if you know wether or not you're working with dos or bash etc.
  • FYI, both Windows and Linux have where, with the same arguments
  • Oops, Windows has where, Ubuntu would have whereis, same thing, different name. Again, nothing works everywhere
  • whereis on Mac OS doesn't seem to locate commands in /usr/local/bin (which is in my PATH env var). But which does.
  • Edited answer to include Mac OS.
  • I get an error Error: spawn where ls ENOENT on macos.
  • @loretoparisi - ENOENT means no such file or directory, and as it says in the comment above, whereis doesn't locate commands on MacOS, you'll need to include which somehow
  • I suppose I could iterate over all paths, but that seems a bit inefficient. Would like for there to be a better solution, but I'll keep this in mind.
  • @dthree Compared to spawning child processes, walking through PATH is more efficient. I implemented a NPM to do that npmjs.com/package/lookpath See stackoverflow.com/a/56321089/1368868 for more information. Thanks.