• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html><html><head>
2<meta charset="utf-8">
3<title>npx</title>
4<style>
5body {
6    background-color: #ffffff;
7    color: #24292e;
8
9    margin: 0;
10
11    line-height: 1.5;
12
13    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
14}
15#rainbar {
16    height: 10px;
17    background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
18}
19
20a {
21    text-decoration: none;
22    color: #0366d6;
23}
24a:hover {
25    text-decoration: underline;
26}
27
28pre {
29    margin: 1em 0px;
30    padding: 1em;
31    border: solid 1px #e1e4e8;
32    border-radius: 6px;
33
34    display: block;
35    overflow: auto;
36
37    white-space: pre;
38
39    background-color: #f6f8fa;
40    color: #393a34;
41}
42code {
43    font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
44    font-size: 85%;
45    padding: 0.2em 0.4em;
46    background-color: #f6f8fa;
47    color: #393a34;
48}
49pre > code {
50    padding: 0;
51    background-color: inherit;
52    color: inherit;
53}
54h1, h2, h3 {
55    font-weight: 600;
56}
57
58#logobar {
59    background-color: #333333;
60    margin: 0 auto;
61    padding: 1em 4em;
62}
63#logobar .logo {
64    float: left;
65}
66#logobar .title {
67    font-weight: 600;
68    color: #dddddd;
69    float: left;
70    margin: 5px 0 0 1em;
71}
72#logobar:after {
73    content: "";
74    display: block;
75    clear: both;
76}
77
78#content {
79    margin: 0 auto;
80    padding: 0 4em;
81}
82
83#table_of_contents > h2 {
84    font-size: 1.17em;
85}
86#table_of_contents ul:first-child {
87    border: solid 1px #e1e4e8;
88    border-radius: 6px;
89    padding: 1em;
90    background-color: #f6f8fa;
91    color: #393a34;
92}
93#table_of_contents ul {
94    list-style-type: none;
95    padding-left: 1.5em;
96}
97#table_of_contents li {
98    font-size: 0.9em;
99}
100#table_of_contents li a {
101    color: #000000;
102}
103
104header.title {
105    border-bottom: solid 1px #e1e4e8;
106}
107header.title > h1 {
108    margin-bottom: 0.25em;
109}
110header.title > .description {
111    display: block;
112    margin-bottom: 0.5em;
113    line-height: 1;
114}
115
116footer#edit {
117    border-top: solid 1px #e1e4e8;
118    margin: 3em 0 4em 0;
119    padding-top: 2em;
120}
121</style>
122</head>
123<body>
124<div id="banner">
125<div id="rainbar"></div>
126<div id="logobar">
127<svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
128<polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
129<polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
130</svg>
131<div class="title">
132npm command-line interface
133</div>
134</div>
135</div>
136
137<section id="content">
138<header class="title">
139<h1 id="npx">npx</h1>
140<span class="description">Run a command from a local or remote npm package</span>
141</header>
142
143<section id="table_of_contents">
144<h2 id="table-of-contents">Table of contents</h2>
145<div id="_table_of_contents"><ul><li><a href="#synopsis">Synopsis</a></li><li><a href="#description">Description</a></li><li><a href="#npx-vs-npm-exec"><code>npx</code> vs <code>npm exec</code></a></li><li><a href="#examples">Examples</a></li><li><a href="#compatibility-with-older-npx-versions">Compatibility with Older npx Versions</a></li><li><a href="#see-also">See Also</a></li></ul></div>
146</section>
147
148<div id="_content"><h3 id="synopsis">Synopsis</h3>
149<pre><code class="language-bash">npx -- &lt;pkg&gt;[@&lt;version&gt;] [args...]
150npx --package=&lt;pkg&gt;[@&lt;version&gt;] -- &lt;cmd&gt; [args...]
151npx -c '&lt;cmd&gt; [args...]'
152npx --package=foo -c '&lt;cmd&gt; [args...]'
153</code></pre>
154<h3 id="description">Description</h3>
155<p>This command allows you to run an arbitrary command from an npm package
156(either one installed locally, or fetched remotely), in a similar context
157as running it via <code>npm run</code>.</p>
158<p>Whatever packages are specified by the <code>--package</code> option will be
159provided in the <code>PATH</code> of the executed command, along with any locally
160installed package executables.  The <code>--package</code> option may be
161specified multiple times, to execute the supplied command in an environment
162where all specified packages are available.</p>
163<p>If any requested packages are not present in the local project
164dependencies, then they are installed to a folder in the npm cache, which
165is added to the <code>PATH</code> environment variable in the executed process.  A
166prompt is printed (which can be suppressed by providing either <code>--yes</code> or
167<code>--no</code>).</p>
168<p>Package names provided without a specifier will be matched with whatever
169version exists in the local project.  Package names with a specifier will
170only be considered a match if they have the exact same name and version as
171the local dependency.</p>
172<p>If no <code>-c</code> or <code>--call</code> option is provided, then the positional arguments
173are used to generate the command string.  If no <code>--package</code> options
174are provided, then npm will attempt to determine the executable name from
175the package specifier provided as the first positional argument according
176to the following heuristic:</p>
177<ul>
178<li>If the package has a single entry in its <code>bin</code> field in <code>package.json</code>,
179or if all entries are aliases of the same command, then that command
180will be used.</li>
181<li>If the package has multiple <code>bin</code> entries, and one of them matches the
182unscoped portion of the <code>name</code> field, then that command will be used.</li>
183<li>If this does not result in exactly one option (either because there are
184no bin entries, or none of them match the <code>name</code> of the package), then
185<code>npm exec</code> exits with an error.</li>
186</ul>
187<p>To run a binary <em>other than</em> the named binary, specify one or more
188<code>--package</code> options, which will prevent npm from inferring the package from
189the first command argument.</p>
190<h3 id="npx-vs-npm-exec"><code>npx</code> vs <code>npm exec</code></h3>
191<p>When run via the <code>npx</code> binary, all flags and options <em>must</em> be set prior to
192any positional arguments.  When run via <code>npm exec</code>, a double-hyphen <code>--</code>
193flag can be used to suppress npm's parsing of switches and options that
194should be sent to the executed command.</p>
195<p>For example:</p>
196<pre><code>$ npx foo@latest bar --package=@npmcli/foo
197</code></pre>
198<p>In this case, npm will resolve the <code>foo</code> package name, and run the
199following command:</p>
200<pre><code>$ foo bar --package=@npmcli/foo
201</code></pre>
202<p>Since the <code>--package</code> option comes <em>after</em> the positional arguments, it is
203treated as an argument to the executed command.</p>
204<p>In contrast, due to npm's argument parsing logic, running this command is
205different:</p>
206<pre><code>$ npm exec foo@latest bar --package=@npmcli/foo
207</code></pre>
208<p>In this case, npm will parse the <code>--package</code> option first, resolving the
209<code>@npmcli/foo</code> package.  Then, it will execute the following command in that
210context:</p>
211<pre><code>$ foo@latest bar
212</code></pre>
213<p>The double-hyphen character is recommended to explicitly tell npm to stop
214parsing command line options and switches.  The following command would
215thus be equivalent to the <code>npx</code> command above:</p>
216<pre><code>$ npm exec -- foo@latest bar --package=@npmcli/foo
217</code></pre>
218<h3 id="examples">Examples</h3>
219<p>Run the version of <code>tap</code> in the local dependencies, with the provided
220arguments:</p>
221<pre><code>$ npm exec -- tap --bail test/foo.js
222$ npx tap --bail test/foo.js
223</code></pre>
224<p>Run a command <em>other than</em> the command whose name matches the package name
225by specifying a <code>--package</code> option:</p>
226<pre><code>$ npm exec --package=foo -- bar --bar-argument
227# ~ or ~
228$ npx --package=foo bar --bar-argument
229</code></pre>
230<p>Run an arbitrary shell script, in the context of the current project:</p>
231<pre><code>$ npm x -c 'eslint &amp;&amp; say "hooray, lint passed"'
232$ npx -c 'eslint &amp;&amp; say "hooray, lint passed"'
233</code></pre>
234<h3 id="compatibility-with-older-npx-versions">Compatibility with Older npx Versions</h3>
235<p>The <code>npx</code> binary was rewritten in npm v7.0.0, and the standalone <code>npx</code>
236package deprecated at that time.  <code>npx</code> uses the <code>npm exec</code>
237command instead of a separate argument parser and install process, with
238some affordances to maintain backwards compatibility with the arguments it
239accepted in previous versions.</p>
240<p>This resulted in some shifts in its functionality:</p>
241<ul>
242<li>Any <code>npm</code> config value may be provided.</li>
243<li>To prevent security and user-experience problems from mistyping package
244names, <code>npx</code> prompts before installing anything.  Suppress this
245prompt with the <code>-y</code> or <code>--yes</code> option.</li>
246<li>The <code>--no-install</code> option is deprecated, and will be converted to <code>--no</code>.</li>
247<li>Shell fallback functionality is removed, as it is not advisable.</li>
248<li>The <code>-p</code> argument is a shorthand for <code>--parseable</code> in npm, but shorthand
249for <code>--package</code> in npx.  This is maintained, but only for the <code>npx</code>
250executable.</li>
251<li>The <code>--ignore-existing</code> option is removed.  Locally installed bins are
252always present in the executed process <code>PATH</code>.</li>
253<li>The <code>--npm</code> option is removed.  <code>npx</code> will always use the <code>npm</code> it ships
254with.</li>
255<li>The <code>--node-arg</code> and <code>-n</code> options have been removed. Use <a href="https://nodejs.org/api/cli.html#node_optionsoptions"><code>NODE_OPTIONS</code></a> instead: e.g.,
256<code>NODE_OPTIONS="--trace-warnings --trace-exit" npx foo --random=true</code></li>
257<li>The <code>--always-spawn</code> option is redundant, and thus removed.</li>
258<li>The <code>--shell</code> option is replaced with <code>--script-shell</code>, but maintained
259in the <code>npx</code> executable for backwards compatibility.</li>
260</ul>
261<h3 id="see-also">See Also</h3>
262<ul>
263<li><a href="../commands/npm-run-script.html">npm run-script</a></li>
264<li><a href="../using-npm/scripts.html">npm scripts</a></li>
265<li><a href="../commands/npm-test.html">npm test</a></li>
266<li><a href="../commands/npm-start.html">npm start</a></li>
267<li><a href="../commands/npm-restart.html">npm restart</a></li>
268<li><a href="../commands/npm-stop.html">npm stop</a></li>
269<li><a href="../commands/npm-config.html">npm config</a></li>
270<li><a href="../commands/npm-exec.html">npm exec</a></li>
271</ul></div>
272
273<footer id="edit">
274<a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npx.md">
275<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
276<path fill-rule="evenodd" d="M11.013 1.427a1.75 1.75 0 012.474 0l1.086 1.086a1.75 1.75 0 010 2.474l-8.61 8.61c-.21.21-.47.364-.756.445l-3.251.93a.75.75 0 01-.927-.928l.929-3.25a1.75 1.75 0 01.445-.758l8.61-8.61zm1.414 1.06a.25.25 0 00-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 000-.354l-1.086-1.086zM11.189 6.25L9.75 4.81l-6.286 6.287a.25.25 0 00-.064.108l-.558 1.953 1.953-.558a.249.249 0 00.108-.064l6.286-6.286z"></path>
277</svg>
278Edit this page on GitHub
279</a>
280</footer>
281</section>
282
283
284
285</body></html>