Get your PATH prepended with locally installed binaries
npx @tessl/cli install tessl/npm-run-path@5.3.0Get your PATH prepended with locally installed binaries. This utility provides the same PATH augmentation that npm run scripts use internally, enabling execution of locally installed binaries by name outside of npm run scripts.
npm install npm-run-pathimport { npmRunPath, npmRunPathEnv, type ProcessEnv, type RunPathOptions, type EnvOptions } from 'npm-run-path';For CommonJS:
const { npmRunPath, npmRunPathEnv } = require('npm-run-path');import childProcess from 'node:child_process';
import { npmRunPath, npmRunPathEnv } from 'npm-run-path';
console.log(process.env.PATH);
//=> '/usr/local/bin'
console.log(npmRunPath());
//=> '/Users/sindresorhus/dev/foo/node_modules/.bin:/Users/sindresorhus/dev/node_modules/.bin:/Users/sindresorhus/node_modules/.bin:/Users/node_modules/.bin:/node_modules/.bin:/usr/local/bin'
// Execute locally installed binary using augmented PATH string
childProcess.exec('eslint --version', { env: { ...process.env, PATH: npmRunPath() } });
// Or use the environment object directly
childProcess.execFileSync('eslint', ['--version'], {
env: npmRunPathEnv()
});Generate an augmented PATH string with locally installed binaries prepended.
export function npmRunPath(options?: RunPathOptions): string;Usage Example:
import { npmRunPath } from 'npm-run-path';
// Default behavior - current directory, include local binaries
const path = npmRunPath();
// Custom working directory
const customPath = npmRunPath({ cwd: '/path/to/project' });
// Exclude locally installed binaries
const globalOnlyPath = npmRunPath({ preferLocal: false });
// Custom PATH to append (instead of process.env.PATH)
const customBasePath = npmRunPath({ path: '/usr/bin:/bin' });
// Exclude Node.js executable directory
const noExecPath = npmRunPath({ addExecPath: false });Generate an augmented environment object with modified PATH.
export function npmRunPathEnv(options?: EnvOptions): ProcessEnv;Usage Example:
import childProcess from 'node:child_process';
import { npmRunPathEnv } from 'npm-run-path';
// Use with child_process
childProcess.execFileSync('eslint', ['--version'], {
env: npmRunPathEnv()
});
// Custom environment base
const customEnv = npmRunPathEnv({
env: { NODE_ENV: 'production', PATH: '/custom/path' }
});
// Custom working directory
const projectEnv = npmRunPathEnv({ cwd: '/path/to/project' });type CommonOptions = {
/**
Working directory.
@default process.cwd()
*/
readonly cwd?: string | URL;
/**
The path to the current Node.js executable.
This can be either an absolute path or a path relative to the `cwd` option.
@default [process.execPath](https://nodejs.org/api/process.html#processexecpath)
*/
readonly execPath?: string | URL;
/**
Whether to push the current Node.js executable's directory (`execPath` option) to the front of PATH.
@default true
*/
readonly addExecPath?: boolean;
/**
Whether to push the locally installed binaries' directory to the front of PATH.
@default true
*/
readonly preferLocal?: boolean;
};
export type RunPathOptions = CommonOptions & {
/**
PATH to be appended.
Set it to an empty string to exclude the default PATH.
@default [`PATH`](https://github.com/sindresorhus/path-key)
*/
readonly path?: string;
};
export type ProcessEnv = Record<string, string | undefined>;
export type EnvOptions = CommonOptions & {
/**
Accepts an object of environment variables, like `process.env`, and modifies the PATH using the correct [PATH key](https://github.com/sindresorhus/path-key). Use this if you're modifying the PATH for use in the `child_process` options.
@default [process.env](https://nodejs.org/api/process.html#processenv)
*/
readonly env?: ProcessEnv;
};The package automatically discovers and prepends node_modules/.bin directories by:
cwd)node_modules/.bin to the PATHpath)This creates a PATH that prioritizes locally installed binaries over global ones, matching npm run script behavior.