which vs env-paths vs cross-env vs command-exists vs npm-which
Node.js Command-Line Utilities
whichenv-pathscross-envcommand-existsnpm-whichSimilar Packages:
Node.js Command-Line Utilities

Node.js Command-Line Utilities are libraries that provide tools for interacting with the command line, managing environment variables, and handling executable files. These utilities help developers create scripts and applications that can perform tasks in the terminal, such as checking for the existence of commands, setting environment variables across platforms, and resolving the paths of executable files. They are essential for building CLI (Command-Line Interface) tools, automating tasks, and ensuring cross-platform compatibility in Node.js applications.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
which158,370,6553457.48 kB5a month agoISC
env-paths34,041,105414-04 years agoMIT
cross-env12,442,1316,51820.2 kB12 months agoMIT
command-exists4,110,210147-176 years agoMIT
npm-which700,92178-19 years agoMIT
Feature Comparison: which vs env-paths vs cross-env vs command-exists vs npm-which

Command Existence Check

  • which:

    which offers a similar functionality by locating the path of a specified executable. It returns the path of the command if found, or an error if it is not. This package is more focused on finding the location of the executable rather than just checking its existence.

  • command-exists:

    command-exists provides a simple API to check if a specific command is available in the user's PATH. It returns a promise that resolves to true if the command exists, or false if it does not. This is useful for scripts that need to verify the availability of tools before executing commands.

Cross-Platform Compatibility

  • env-paths:

    env-paths provides platform-agnostic methods for retrieving environment-specific paths, ensuring consistency across operating systems. It does not set variables but helps applications access the correct paths based on the user's platform.

  • cross-env:

    cross-env is designed to handle cross-platform differences in setting environment variables. It allows you to set variables in a way that works on both Windows and Unix-like systems, making it ideal for scripts in package.json that need to run in diverse environments.

Executable Path Resolution

  • which:

    which also resolves the path of an executable but does so in a straightforward manner. It is a lightweight implementation that quickly finds the location of a command in the user's PATH.

  • npm-which:

    npm-which resolves the path of an executable by searching the user's PATH environment variable. It is particularly useful for Node.js applications that need to find the location of installed command-line tools.

Environment Variable Management

  • cross-env:

    cross-env allows you to set environment variables directly in your scripts, ensuring they are available regardless of the operating system. This is especially useful for build scripts and CI/CD pipelines where consistent variable management is required.

Ease of Use: Code Examples

  • which:

    Find the path of a command using which

    const which = require('which');
    
    which('node', (err, path) => {
      if (err) {
        console.error('Command not found.');
      } else {
        console.log(`Command path: ${path}`);
      }
    });
    
  • command-exists:

    Check if a command exists using command-exists

    const commandExists = require('command-exists');
    
    commandExists('git')
      .then(() => console.log('Command exists!'))
      .catch(() => console.log('Command does not exist.'));
    
How to Choose: which vs env-paths vs cross-env vs command-exists vs npm-which
  • which:

    Select which if you need a straightforward and efficient way to find the location of a command-line executable. This package is a direct implementation of the Unix which command, making it fast and reliable for locating executables in the user's PATH.

  • env-paths:

    Use env-paths if you need to reliably retrieve platform-specific paths for configuration files, caches, and other environment-related directories. This package is useful for applications that need to store user-specific or application-specific data in a consistent manner across different operating systems.

  • cross-env:

    Select cross-env if you need to set environment variables in a cross-platform manner, especially when working with scripts in package.json. It is particularly useful for ensuring that environment variables are set correctly on both Windows and Unix-like systems, making it a must-have for teams working in diverse environments.

  • command-exists:

    Choose command-exists if you need a simple and reliable way to check if a specific command-line tool is installed on the user's system. It is lightweight and easy to use, making it ideal for scripts and applications that require this functionality without any overhead.

  • npm-which:

    Choose npm-which if you need a reliable way to find the path of an executable file in the user's PATH. It is particularly useful for Node.js applications that need to locate command-line tools or scripts, ensuring that you get the correct path regardless of the user's environment.

README for which

which

Like the unix which utility.

Finds the first instance of a specified executable in the PATH environment variable. Does not cache the results, so hash -r is not needed when the PATH changes.

USAGE

const which = require('which')

// async usage
// rejects if not found
const resolved = await which('node')

// if nothrow option is used, returns null if not found
const resolvedOrNull = await which('node', { nothrow: true })

// sync usage
// throws if not found
const resolved = which.sync('node')

// if nothrow option is used, returns null if not found
const resolvedOrNull = which.sync('node', { nothrow: true })

// Pass options to override the PATH and PATHEXT environment vars.
await which('node', { path: someOtherPath, pathExt: somePathExt })

CLI USAGE

Just like the BSD which(1) binary but using node-which.

usage: node-which [-as] program ...

You can learn more about why the binary is node-which and not which here

OPTIONS

You may pass an options object as the second argument.

  • path: Use instead of the PATH environment variable.
  • pathExt: Use instead of the PATHEXT environment variable.
  • all: Return all matches, instead of just the first one. Note that this means the function returns an array of strings instead of a single string.