or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-run-path

Get your PATH prepended with locally installed binaries

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/npm-run-path@5.3.x

To install, run

npx @tessl/cli install tessl/npm-run-path@5.3.0

index.mddocs/

npm-run-path

Get 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.

Package Information

  • Package Name: npm-run-path
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install npm-run-path

Core Imports

import { npmRunPath, npmRunPathEnv, type ProcessEnv, type RunPathOptions, type EnvOptions } from 'npm-run-path';

For CommonJS:

const { npmRunPath, npmRunPathEnv } = require('npm-run-path');

Basic Usage

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()
});

Capabilities

PATH String Generation

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 });

Environment Object Generation

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' });

Types

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;
};

How It Works

The package automatically discovers and prepends node_modules/.bin directories by:

  1. Starting from the current working directory (or specified cwd)
  2. Adding node_modules/.bin to the PATH
  3. Moving up one directory level and repeating until reaching the filesystem root
  4. Optionally adding the Node.js executable's directory to the PATH
  5. Appending the original PATH (or specified path)

This creates a PATH that prioritizes locally installed binaries over global ones, matching npm run script behavior.

Common Use Cases

  • CLI Tools: Enable CLI tools to execute locally installed binaries
  • Build Scripts: Use in build systems that need to run project-specific tools
  • Child Processes: Spawn processes with access to local binaries
  • Development Tools: Allow development tools to use project-specific versions of utilities