pax_global_header00006660000000000000000000000064146630375340014525gustar00rootroot0000000000000052 comment=dbea877d781b7da037d3f89dfc921aae174feb28 npm-run-path-6.0.0/000077500000000000000000000000001466303753400140565ustar00rootroot00000000000000npm-run-path-6.0.0/.editorconfig000066400000000000000000000002571466303753400165370ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 npm-run-path-6.0.0/.gitattributes000066400000000000000000000000231466303753400167440ustar00rootroot00000000000000* text=auto eol=lf npm-run-path-6.0.0/.github/000077500000000000000000000000001466303753400154165ustar00rootroot00000000000000npm-run-path-6.0.0/.github/security.md000066400000000000000000000002631466303753400176100ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. npm-run-path-6.0.0/.github/workflows/000077500000000000000000000000001466303753400174535ustar00rootroot00000000000000npm-run-path-6.0.0/.github/workflows/main.yml000066400000000000000000000010101466303753400211120ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }} runs-on: ${{ matrix.os }}-latest strategy: fail-fast: false matrix: node-version: - 22 - 18 os: - ubuntu - macos - windows steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test npm-run-path-6.0.0/.gitignore000066400000000000000000000000271466303753400160450ustar00rootroot00000000000000node_modules yarn.lock npm-run-path-6.0.0/.npmrc000066400000000000000000000000231466303753400151710ustar00rootroot00000000000000package-lock=false npm-run-path-6.0.0/index.d.ts000066400000000000000000000045771466303753400157740ustar00rootroot00000000000000type 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; 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; }; /** Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries. @returns The augmented path string. @example ``` import childProcess from 'node:child_process'; import {npmRunPath} 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' ``` */ export function npmRunPath(options?: RunPathOptions): string; /** Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries. @returns The augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object. @example ``` import childProcess from 'node:child_process'; import {npmRunPathEnv} from 'npm-run-path'; // `foo` is a locally installed binary childProcess.execFileSync('foo', { env: npmRunPathEnv() }); ``` */ export function npmRunPathEnv(options?: EnvOptions): ProcessEnv; npm-run-path-6.0.0/index.js000066400000000000000000000026761466303753400155360ustar00rootroot00000000000000import process from 'node:process'; import path from 'node:path'; import pathKey from 'path-key'; import {toPath, traversePathUp} from 'unicorn-magic'; export const npmRunPath = ({ cwd = process.cwd(), path: pathOption = process.env[pathKey()], preferLocal = true, execPath = process.execPath, addExecPath = true, } = {}) => { const cwdPath = path.resolve(toPath(cwd)); const result = []; const pathParts = pathOption.split(path.delimiter); if (preferLocal) { applyPreferLocal(result, pathParts, cwdPath); } if (addExecPath) { applyExecPath(result, pathParts, execPath, cwdPath); } return pathOption === '' || pathOption === path.delimiter ? `${result.join(path.delimiter)}${pathOption}` : [...result, pathOption].join(path.delimiter); }; const applyPreferLocal = (result, pathParts, cwdPath) => { for (const directory of traversePathUp(cwdPath)) { const pathPart = path.join(directory, 'node_modules/.bin'); if (!pathParts.includes(pathPart)) { result.push(pathPart); } } }; // Ensure the running `node` binary is used const applyExecPath = (result, pathParts, execPath, cwdPath) => { const pathPart = path.resolve(cwdPath, toPath(execPath), '..'); if (!pathParts.includes(pathPart)) { result.push(pathPart); } }; export const npmRunPathEnv = ({env = process.env, ...options} = {}) => { env = {...env}; const pathName = pathKey({env}); options.path = env[pathName]; env[pathName] = npmRunPath(options); return env; }; npm-run-path-6.0.0/index.test-d.ts000066400000000000000000000034441466303753400167410ustar00rootroot00000000000000import process from 'node:process'; import {expectType, expectError} from 'tsd'; import {npmRunPath, npmRunPathEnv, type ProcessEnv} from './index.js'; const fileUrl = new URL('file:///foo'); expectType(npmRunPath()); expectType(npmRunPath({cwd: '/foo'})); expectType(npmRunPath({cwd: fileUrl})); expectError(npmRunPath({cwd: false})); expectType(npmRunPath({path: '/usr/local/bin'})); expectError(npmRunPath({path: fileUrl})); expectError(npmRunPath({path: false})); expectType(npmRunPath({execPath: '/usr/local/bin'})); expectType(npmRunPath({execPath: fileUrl})); expectError(npmRunPath({execPath: false})); expectType(npmRunPath({addExecPath: false})); expectError(npmRunPath({addExecPath: ''})); expectType(npmRunPath({preferLocal: false})); expectError(npmRunPath({preferLocal: ''})); expectType(npmRunPathEnv()); expectType(npmRunPathEnv({cwd: '/foo'})); expectType(npmRunPathEnv({cwd: fileUrl})); expectError(npmRunPathEnv({cwd: false})); expectType(npmRunPathEnv({env: process.env})); // eslint-disable-line @typescript-eslint/no-unsafe-assignment expectType(npmRunPathEnv({env: {foo: 'bar'}})); expectType(npmRunPathEnv({env: {foo: undefined}})); expectError(npmRunPath({env: false})); expectError(npmRunPath({env: {[Symbol('key')]: 'bar'}})); expectError(npmRunPath({env: {foo: false}})); expectType(npmRunPathEnv({execPath: '/usr/local/bin'})); expectType(npmRunPathEnv({execPath: fileUrl})); expectError(npmRunPath({execPath: false})); expectType(npmRunPathEnv({addExecPath: false})); expectError(npmRunPathEnv({addExecPath: ''})); expectType(npmRunPathEnv({preferLocal: false})); expectError(npmRunPathEnv({preferLocal: ''})); npm-run-path-6.0.0/license000066400000000000000000000021351466303753400154240ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. npm-run-path-6.0.0/package.json000066400000000000000000000016301466303753400163440ustar00rootroot00000000000000{ "name": "npm-run-path", "version": "6.0.0", "description": "Get your PATH prepended with locally installed binaries", "license": "MIT", "repository": "sindresorhus/npm-run-path", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": { "types": "./index.d.ts", "default": "./index.js" }, "sideEffects": false, "engines": { "node": ">=18" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "npm", "run", "path", "package", "bin", "binary", "binaries", "script", "cli", "command-line", "execute", "executable" ], "dependencies": { "path-key": "^4.0.0", "unicorn-magic": "^0.3.0" }, "devDependencies": { "ava": "^6.1.3", "tsd": "^0.31.1", "xo": "^0.59.3" } } npm-run-path-6.0.0/readme.md000066400000000000000000000052221466303753400156360ustar00rootroot00000000000000# npm-run-path > Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries In [npm run scripts](https://docs.npmjs.com/cli/run-script) you can execute locally installed binaries by name. This enables the same outside npm. ## Install ```sh npm install npm-run-path ``` ## Usage ```js 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' // `foo` is a locally installed binary childProcess.execFileSync('foo', { env: npmRunPathEnv() }); ``` ## API ### npmRunPath(options?) `options`: [`Options`](#options)\ _Returns_: `string` Returns the augmented PATH string. ### npmRunPathEnv(options?) `options`: [`Options`](#options)\ _Returns_: `object` Returns the augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object. ### options Type: `object` #### cwd Type: `string | URL`\ Default: `process.cwd()` The working directory. #### execPath Type: `string | URL`\ Default: [`process.execPath`](https://nodejs.org/api/process.html#processexecpath) The path to the current Node.js executable. This can be either an absolute path or a path relative to the [`cwd` option](#cwd). #### addExecPath Type: `boolean`\ Default: `true` Whether to push the current Node.js executable's directory ([`execPath`](#execpath) option) to the front of PATH. #### preferLocal Type: `boolean`\ Default: `true` Whether to push the locally installed binaries' directory to the front of PATH. #### path Type: `string`\ Default: [`PATH`](https://github.com/sindresorhus/path-key) The PATH to be appended. Set it to an empty string to exclude the default PATH. Only available with [`npmRunPath()`](#npmrunpathoptions), not [`npmRunPathEnv()`](#npmrunpathenvoptions). #### env Type: `object`\ Default: [`process.env`](https://nodejs.org/api/process.html#processenv) 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. Only available with [`npmRunPathEnv()`](#npmrunpathenvoptions), not [`npmRunPath()`](#npmrunpathoptions). ## Related - [npm-run-path-cli](https://github.com/sindresorhus/npm-run-path-cli) - CLI for this module - [execa](https://github.com/sindresorhus/execa) - Execute a locally installed binary npm-run-path-6.0.0/test.js000066400000000000000000000127171466303753400154030ustar00rootroot00000000000000import process from 'node:process'; import path from 'node:path'; import {fileURLToPath, pathToFileURL} from 'node:url'; import test from 'ava'; import {npmRunPath, npmRunPathEnv} from './index.js'; const localBinaryDirectory = fileURLToPath(new URL('node_modules/.bin', import.meta.url)); const testLocalDirectory = (t, addExecPath, preferLocal, expectedResult) => { t.is( npmRunPath({path: '', addExecPath, preferLocal}).split(path.delimiter)[0] === localBinaryDirectory, expectedResult, ); }; test('Adds node_modules/.bin - npmRunPath()', testLocalDirectory, undefined, undefined, true); test('"addExecPath: false" still adds node_modules/.bin - npmRunPath()', testLocalDirectory, false, undefined, true); test('"preferLocal: false" does not add node_modules/.bin - npmRunPath()', testLocalDirectory, undefined, false, false); test('"preferLocal: false", "addExecPath: false" does not add node_modules/.bin - npmRunPath()', testLocalDirectory, false, false, false); const testLocalDirectoryEnv = (t, addExecPath, preferLocal, expectedResult) => { t.is( npmRunPathEnv({env: {PATH: 'foo'}, addExecPath, preferLocal}).PATH.split(path.delimiter)[0] === localBinaryDirectory, expectedResult, ); }; test('Adds node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, undefined, undefined, true); test('"addExecPath: false" still adds node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, false, undefined, true); test('"preferLocal: false" does not add node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, undefined, false, false); test('"preferLocal: false", "addExecPath: false" does not add node_modules/.bin - npmRunPathEnv()', testLocalDirectoryEnv, false, false, false); test('node_modules/.bin is not added twice', t => { const firstPathEnv = npmRunPath({path: ''}); const pathEnv = npmRunPath({path: firstPathEnv}); const execPaths = pathEnv .split(path.delimiter) .filter(pathPart => pathPart === localBinaryDirectory); t.is(execPaths.length, 1); }); test('the `cwd` option changes the current directory', t => { t.is( npmRunPath({path: '', cwd: './dir'}).split(path.delimiter)[0], path.resolve('./dir/node_modules/.bin'), ); }); test('the `cwd` option can be a file URL', t => { t.is( npmRunPath({path: '', cwd: new URL('dir', import.meta.url)}).split(path.delimiter)[0], fileURLToPath(new URL('dir/node_modules/.bin', import.meta.url)), ); }); test('push `execPath` later in the PATH', t => { const pathEnv = npmRunPath({path: ''}).split(path.delimiter); t.is(pathEnv.at(-1), path.dirname(process.execPath)); }); test('`execPath` is not added twice', t => { const firstPathEnv = npmRunPath({path: ''}); const pathEnv = npmRunPath({path: firstPathEnv}); const execPaths = pathEnv .split(path.delimiter) .filter(pathPart => pathPart === path.dirname(process.execPath)); t.is(execPaths.length, 1); }); const testExecPath = (t, preferLocal, addExecPath, expectedResult) => { const pathEnv = npmRunPath({ path: '', execPath: 'test/test', preferLocal, addExecPath, }).split(path.delimiter); t.is(pathEnv.at(-1) === path.resolve('test'), expectedResult); }; test('can change `execPath` with the `execPath` option - npmRunPath()', testExecPath, undefined, undefined, true); test('"preferLocal: false" still adds execPath - npmRunPath()', testExecPath, false, undefined, true); test('"addExecPath: false" does not add execPath - npmRunPath()', testExecPath, undefined, false, false); test('"addExecPath: false", "preferLocal: false" does not add execPath - npmRunPath()', testExecPath, false, false, false); const testExecPathEnv = (t, preferLocal, addExecPath, expectedResult) => { const pathEnv = npmRunPathEnv({ env: {PATH: 'foo'}, execPath: 'test/test', preferLocal, addExecPath, }).PATH.split(path.delimiter); t.is(pathEnv.at(-2) === path.resolve('test'), expectedResult); }; test('can change `execPath` with the `execPath` option - npmRunPathEnv()', testExecPathEnv, undefined, undefined, true); test('"preferLocal: false" still adds execPath - npmRunPathEnv()', testExecPathEnv, false, undefined, true); test('"addExecPath: false" does not add execPath - npmRunPathEnv()', testExecPathEnv, undefined, false, false); test('"addExecPath: false", "preferLocal: false" does not add execPath - npmRunPathEnv()', testExecPathEnv, false, false, false); test('the `execPath` option can be a file URL', t => { const pathEnv = npmRunPath({path: '', execPath: pathToFileURL('test/test')}).split(path.delimiter); t.is(pathEnv.at(-1), path.resolve('test')); }); test('the `execPath` option is relative to the `cwd` option', t => { const pathEnv = npmRunPath({ path: '', execPath: 'test/test', cwd: './dir', }).split(path.delimiter); t.is(pathEnv.at(-1), path.resolve('./dir/test')); }); test('the PATH can remain empty', t => { t.is(npmRunPath({path: '', preferLocal: false, addExecPath: false}), ''); }); const testEmptyPath = (t, pathValue, shouldEndWithDelimiter, hasTwoDelimiters) => { const pathEnv = npmRunPath({path: pathValue}); t.not(pathEnv, ''); t.false(pathEnv.startsWith(path.delimiter)); t.is(pathEnv.endsWith(path.delimiter), shouldEndWithDelimiter); t.is(pathEnv.includes(`${path.delimiter}${path.delimiter}`), hasTwoDelimiters); }; test('the PATH can be empty', testEmptyPath, '', false, false); test('the PATH can be ; or :', testEmptyPath, path.delimiter, true, false); test('the PATH can start with ; or :', testEmptyPath, `${path.delimiter}foo`, false, true); test('the PATH can end with ; or :', testEmptyPath, `foo${path.delimiter}`, true, false);