UNPKG

2.43 kBTypeScriptView Raw
1type CommonOptions = {
2 /**
3 Working directory.
4
5 @default process.cwd()
6 */
7 readonly cwd?: string | URL;
8
9 /**
10 The path to the current Node.js executable.
11
12 This can be either an absolute path or a path relative to the `cwd` option.
13
14 @default [process.execPath](https://nodejs.org/api/process.html#processexecpath)
15 */
16 readonly execPath?: string | URL;
17
18 /**
19 Whether to push the current Node.js executable's directory (`execPath` option) to the front of PATH.
20
21 @default true
22 */
23 readonly addExecPath?: boolean;
24
25 /**
26 Whether to push the locally installed binaries' directory to the front of PATH.
27
28 @default true
29 */
30 readonly preferLocal?: boolean;
31};
32
33export type RunPathOptions = CommonOptions & {
34 /**
35 PATH to be appended.
36
37 Set it to an empty string to exclude the default PATH.
38
39 @default [`PATH`](https://github.com/sindresorhus/path-key)
40 */
41 readonly path?: string;
42};
43
44export type ProcessEnv = Record<string, string | undefined>;
45
46export type EnvOptions = CommonOptions & {
47 /**
48 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.
49
50 @default [process.env](https://nodejs.org/api/process.html#processenv)
51 */
52 readonly env?: ProcessEnv;
53};
54
55/**
56Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries.
57
58@returns The augmented path string.
59
60@example
61```
62import childProcess from 'node:child_process';
63import {npmRunPath} from 'npm-run-path';
64
65console.log(process.env.PATH);
66//=> '/usr/local/bin'
67
68console.log(npmRunPath());
69//=> '/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'
70```
71*/
72export function npmRunPath(options?: RunPathOptions): string;
73
74/**
75Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries.
76
77@returns The augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object.
78
79@example
80```
81import childProcess from 'node:child_process';
82import {npmRunPathEnv} from 'npm-run-path';
83
84// `foo` is a locally installed binary
85childProcess.execFileSync('foo', {
86 env: npmRunPathEnv()
87});
88```
89*/
90export function npmRunPathEnv(options?: EnvOptions): ProcessEnv;