UNPKG

2.85 kBMarkdownView Raw
1# npm-run-path
2
3> Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries
4
5In [npm run scripts](https://docs.npmjs.com/cli/run-script) you can execute locally installed binaries by name. This enables the same outside npm.
6
7## Install
8
9```sh
10npm install npm-run-path
11```
12
13## Usage
14
15```js
16import childProcess from 'node:child_process';
17import {npmRunPath, npmRunPathEnv} from 'npm-run-path';
18
19console.log(process.env.PATH);
20//=> '/usr/local/bin'
21
22console.log(npmRunPath());
23//=> '/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'
24
25// `foo` is a locally installed binary
26childProcess.execFileSync('foo', {
27 env: npmRunPathEnv()
28});
29```
30
31## API
32
33### npmRunPath(options?)
34
35Returns the augmented PATH string.
36
37#### options
38
39Type: `object`
40
41##### cwd
42
43Type: `string | URL`\
44Default: `process.cwd()`
45
46The working directory.
47
48##### path
49
50Type: `string`\
51Default: [`PATH`](https://github.com/sindresorhus/path-key)
52
53The PATH to be appended.
54
55Set it to an empty string to exclude the default PATH.
56
57##### execPath
58
59Type: `string`\
60Default: `process.execPath`
61
62The path to the current Node.js executable. Its directory is pushed to the front of PATH.
63
64This can be either an absolute path or a path relative to the [`cwd` option](#cwd).
65
66### npmRunPathEnv(options?)
67
68Returns the augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object.
69
70#### options
71
72Type: `object`
73
74##### cwd
75
76Type: `string | URL`\
77Default: `process.cwd()`
78
79The working directory.
80
81##### env
82
83Type: `object`
84
85Accepts 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.
86
87##### execPath
88
89Type: `string`\
90Default: `process.execPath`
91
92The path to the Node.js executable to use in child processes if that is different from the current one. Its directory is pushed to the front of PATH.
93
94This can be either an absolute path or a path relative to the [`cwd` option](#cwd).
95
96## Related
97
98- [npm-run-path-cli](https://github.com/sindresorhus/npm-run-path-cli) - CLI for this module
99- [execa](https://github.com/sindresorhus/execa) - Execute a locally installed binary
100
101---
102
103<div align="center">
104 <b>
105 <a href="https://tidelift.com/subscription/pkg/npm-npm-run-path?utm_source=npm-npm-run-path&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
106 </b>
107 <br>
108 <sub>
109 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
110 </sub>
111</div>