Promisified Node Which

1.0.0 · active · verified Tue Apr 21

which-promise is a JavaScript utility library that provides a Promise-based API for locating executable programs within a user's system `PATH` environment variable. It functions as a direct, promisified wrapper around the robust and widely used `node-which` library, offering a modern, asynchronous interface to a fundamental system operation. The package is currently at version 1.0.0, indicating a stable and mature API that is unlikely to see frequent breaking changes. As a wrapper, its release cadence is closely tied to updates and bug fixes in its underlying dependency, `node-which`, primarily focusing on stability and compatibility across different Node.js versions and operating systems. Its primary differentiator is simplifying the asynchronous lookup of executables by leveraging native JavaScript Promises, making it straightforward to integrate into contemporary `async/await` workflows, which is a significant improvement over traditional callback-based approaches. It is particularly valuable in Node.js development for scripting, tooling, and applications that require programmatic discovery of system binaries.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `which-promise` to asynchronously locate common executables like 'node' and 'npm' in the system's PATH, and handles the case where a command is not found.

const whichPromise = require('which-promise');

async function findExecutables() {
  try {
    // Find the 'node' executable in the system's PATH
    const nodePath = await whichPromise('node');
    console.log(`Node.js executable found at: ${nodePath}`);

    // Find the 'npm' executable
    const npmPath = await whichPromise('npm');
    console.log(`npm executable found at: ${npmPath}`);

    // Example of a command that might not exist
    const nonExistentCommand = 'nonexistent-tool-123';
    await whichPromise(nonExistentCommand);
    console.log(`This line will not be reached if '${nonExistentCommand}' is not found.`);
  } catch (err) {
    if (err.message.startsWith('not found')) {
      console.error(`Error: ${err.message}`);
    } else {
      console.error(`An unexpected error occurred: ${err.message}`);
    }
  }
}

findExecutables();

view raw JSON →