1<!DOCTYPE html><html><head> 2<meta charset="utf-8"> 3<title>npm-exec</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="npm-exec">npm-exec</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="#configuration">Configuration</a></li><ul><li><a href="#package"><code>package</code></a></li><li><a href="#call"><code>call</code></a></li><li><a href="#workspace"><code>workspace</code></a></li><li><a href="#workspaces"><code>workspaces</code></a></li><li><a href="#include-workspace-root"><code>include-workspace-root</code></a></li></ul><li><a href="#examples">Examples</a></li><li><a href="#workspaces-support">Workspaces support</a></li><ul><li><a href="#filtering-workspaces">Filtering workspaces</a></li></ul><li><a href="#compatibility-with-older-npx-versions">Compatibility with Older npx Versions</a></li><li><a href="#a-note-on-caching">A note on caching</a></li><ul><li><a href="#prefer-online">prefer-online</a></li><li><a href="#prefer-offline">prefer-offline</a></li><li><a href="#offline">offline</a></li><li><a href="#workspace2">workspace</a></li><li><a href="#workspaces2">workspaces</a></li></ul><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">npm exec -- <pkg>[@<version>] [args...] 150npm exec --package=<pkg>[@<version>] -- <cmd> [args...] 151npm exec -c '<cmd> [args...]' 152npm exec --package=foo -c '<cmd> [args...]' 153 154alias: x 155</code></pre> 156<h3 id="description">Description</h3> 157<p>This command allows you to run an arbitrary command from an npm package 158(either one installed locally, or fetched remotely), in a similar context 159as running it via <code>npm run</code>.</p> 160<p>Run without positional arguments or <code>--call</code>, this allows you to 161interactively run commands in the same sort of shell environment that 162<code>package.json</code> scripts are run. Interactive mode is not supported in CI 163environments when standard input is a TTY, to prevent hangs.</p> 164<p>Whatever packages are specified by the <code>--package</code> option will be 165provided in the <code>PATH</code> of the executed command, along with any locally 166installed package executables. The <code>--package</code> option may be 167specified multiple times, to execute the supplied command in an environment 168where all specified packages are available.</p> 169<p>If any requested packages are not present in the local project 170dependencies, then a prompt is printed, which can be suppressed by 171providing either <code>--yes</code> or <code>--no</code>. When standard input is not a TTY or a 172CI environment is detected, <code>--yes</code> is assumed. The requested packages are 173installed to a folder in the npm cache, which is added to the <code>PATH</code> 174environment variable in the executed process.</p> 175<p>Package names provided without a specifier will be matched with whatever 176version exists in the local project. Package names with a specifier will 177only be considered a match if they have the exact same name and version as 178the local dependency.</p> 179<p>If no <code>-c</code> or <code>--call</code> option is provided, then the positional arguments 180are used to generate the command string. If no <code>--package</code> options 181are provided, then npm will attempt to determine the executable name from 182the package specifier provided as the first positional argument according 183to the following heuristic:</p> 184<ul> 185<li>If the package has a single entry in its <code>bin</code> field in <code>package.json</code>, 186or if all entries are aliases of the same command, then that command 187will be used.</li> 188<li>If the package has multiple <code>bin</code> entries, and one of them matches the 189unscoped portion of the <code>name</code> field, then that command will be used.</li> 190<li>If this does not result in exactly one option (either because there are 191no bin entries, or none of them match the <code>name</code> of the package), then 192<code>npm exec</code> exits with an error.</li> 193</ul> 194<p>To run a binary <em>other than</em> the named binary, specify one or more 195<code>--package</code> options, which will prevent npm from inferring the package from 196the first command argument.</p> 197<h3 id="npx-vs-npm-exec"><code>npx</code> vs <code>npm exec</code></h3> 198<p>When run via the <code>npx</code> binary, all flags and options <em>must</em> be set prior to 199any positional arguments. When run via <code>npm exec</code>, a double-hyphen <code>--</code> 200flag can be used to suppress npm's parsing of switches and options that 201should be sent to the executed command.</p> 202<p>For example:</p> 203<pre><code>$ npx foo@latest bar --package=@npmcli/foo 204</code></pre> 205<p>In this case, npm will resolve the <code>foo</code> package name, and run the 206following command:</p> 207<pre><code>$ foo bar --package=@npmcli/foo 208</code></pre> 209<p>Since the <code>--package</code> option comes <em>after</em> the positional arguments, it is 210treated as an argument to the executed command.</p> 211<p>In contrast, due to npm's argument parsing logic, running this command is 212different:</p> 213<pre><code>$ npm exec foo@latest bar --package=@npmcli/foo 214</code></pre> 215<p>In this case, npm will parse the <code>--package</code> option first, resolving the 216<code>@npmcli/foo</code> package. Then, it will execute the following command in that 217context:</p> 218<pre><code>$ foo@latest bar 219</code></pre> 220<p>The double-hyphen character is recommended to explicitly tell npm to stop 221parsing command line options and switches. The following command would 222thus be equivalent to the <code>npx</code> command above:</p> 223<pre><code>$ npm exec -- foo@latest bar --package=@npmcli/foo 224</code></pre> 225<h3 id="configuration">Configuration</h3> 226<h4 id="package"><code>package</code></h4> 227<ul> 228<li>Default:</li> 229<li>Type: String (can be set multiple times)</li> 230</ul> 231<p>The package or packages to install for <a href="../commands/npm-exec.html"><code>npm exec</code></a></p> 232<h4 id="call"><code>call</code></h4> 233<ul> 234<li>Default: ""</li> 235<li>Type: String</li> 236</ul> 237<p>Optional companion option for <code>npm exec</code>, <code>npx</code> that allows for specifying a 238custom command to be run along with the installed packages.</p> 239<pre><code class="language-bash">npm exec --package yo --package generator-node --call "yo node" 240</code></pre> 241<h4 id="workspace"><code>workspace</code></h4> 242<ul> 243<li>Default:</li> 244<li>Type: String (can be set multiple times)</li> 245</ul> 246<p>Enable running a command in the context of the configured workspaces of the 247current project while filtering by running only the workspaces defined by 248this configuration option.</p> 249<p>Valid values for the <code>workspace</code> config are either:</p> 250<ul> 251<li>Workspace names</li> 252<li>Path to a workspace directory</li> 253<li>Path to a parent workspace directory (will result in selecting all 254workspaces within that folder)</li> 255</ul> 256<p>When set for the <code>npm init</code> command, this may be set to the folder of a 257workspace which does not yet exist, to create the folder and set it up as a 258brand new workspace within the project.</p> 259<p>This value is not exported to the environment for child processes.</p> 260<h4 id="workspaces"><code>workspaces</code></h4> 261<ul> 262<li>Default: null</li> 263<li>Type: null or Boolean</li> 264</ul> 265<p>Set to true to run the command in the context of <strong>all</strong> configured 266workspaces.</p> 267<p>Explicitly setting this to false will cause commands like <code>install</code> to 268ignore workspaces altogether. When not set explicitly:</p> 269<ul> 270<li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.) 271will link workspaces into the <code>node_modules</code> folder. - Commands that do 272other things (test, exec, publish, etc.) will operate on the root project, 273<em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li> 274</ul> 275<p>This value is not exported to the environment for child processes.</p> 276<h4 id="include-workspace-root"><code>include-workspace-root</code></h4> 277<ul> 278<li>Default: false</li> 279<li>Type: Boolean</li> 280</ul> 281<p>Include the workspace root when workspaces are enabled for a command.</p> 282<p>When false, specifying individual workspaces via the <code>workspace</code> config, or 283all workspaces via the <code>workspaces</code> flag, will cause npm to operate only on 284the specified workspaces, and not on the root project.</p> 285<p>This value is not exported to the environment for child processes.</p> 286<h3 id="examples">Examples</h3> 287<p>Run the version of <code>tap</code> in the local dependencies, with the provided 288arguments:</p> 289<pre><code>$ npm exec -- tap --bail test/foo.js 290$ npx tap --bail test/foo.js 291</code></pre> 292<p>Run a command <em>other than</em> the command whose name matches the package name 293by specifying a <code>--package</code> option:</p> 294<pre><code>$ npm exec --package=foo -- bar --bar-argument 295# ~ or ~ 296$ npx --package=foo bar --bar-argument 297</code></pre> 298<p>Run an arbitrary shell script, in the context of the current project:</p> 299<pre><code>$ npm x -c 'eslint && say "hooray, lint passed"' 300$ npx -c 'eslint && say "hooray, lint passed"' 301</code></pre> 302<h3 id="workspaces-support">Workspaces support</h3> 303<p>You may use the <a href="../using-npm/config#workspace.html"><code>workspace</code></a> or 304<a href="../using-npm/config#workspaces.html"><code>workspaces</code></a> configs in order to run an 305arbitrary command from an npm package (either one installed locally, or fetched 306remotely) in the context of the specified workspaces. 307If no positional argument or <code>--call</code> option is provided, it will open an 308interactive subshell in the context of each of these configured workspaces one 309at a time.</p> 310<p>Given a project with configured workspaces, e.g:</p> 311<pre><code>. 312+-- package.json 313`-- packages 314 +-- a 315 | `-- package.json 316 +-- b 317 | `-- package.json 318 `-- c 319 `-- package.json 320</code></pre> 321<p>Assuming the workspace configuration is properly set up at the root level 322<code>package.json</code> file. e.g:</p> 323<pre><code>{ 324 "workspaces": [ "./packages/*" ] 325} 326</code></pre> 327<p>You can execute an arbitrary command from a package in the context of each of 328the configured workspaces when using the 329<a href="../using-npm/config#workspace.html"><code>workspaces</code> config options</a>, in this example 330we're using <strong>eslint</strong> to lint any js file found within each workspace folder:</p> 331<pre><code>npm exec --ws -- eslint ./*.js 332</code></pre> 333<h4 id="filtering-workspaces">Filtering workspaces</h4> 334<p>It's also possible to execute a command in a single workspace using the 335<code>workspace</code> config along with a name or directory path:</p> 336<pre><code>npm exec --workspace=a -- eslint ./*.js 337</code></pre> 338<p>The <code>workspace</code> config can also be specified multiple times in order to run a 339specific script in the context of multiple workspaces. When defining values for 340the <code>workspace</code> config in the command line, it also possible to use <code>-w</code> as a 341shorthand, e.g:</p> 342<pre><code>npm exec -w a -w b -- eslint ./*.js 343</code></pre> 344<p>This last command will run the <code>eslint</code> command in both <code>./packages/a</code> and 345<code>./packages/b</code> folders.</p> 346<h3 id="compatibility-with-older-npx-versions">Compatibility with Older npx Versions</h3> 347<p>The <code>npx</code> binary was rewritten in npm v7.0.0, and the standalone <code>npx</code> 348package deprecated at that time. <code>npx</code> uses the <code>npm exec</code> 349command instead of a separate argument parser and install process, with 350some affordances to maintain backwards compatibility with the arguments it 351accepted in previous versions.</p> 352<p>This resulted in some shifts in its functionality:</p> 353<ul> 354<li>Any <code>npm</code> config value may be provided.</li> 355<li>To prevent security and user-experience problems from mistyping package 356names, <code>npx</code> prompts before installing anything. Suppress this 357prompt with the <code>-y</code> or <code>--yes</code> option.</li> 358<li>The <code>--no-install</code> option is deprecated, and will be converted to <code>--no</code>.</li> 359<li>Shell fallback functionality is removed, as it is not advisable.</li> 360<li>The <code>-p</code> argument is a shorthand for <code>--parseable</code> in npm, but shorthand 361for <code>--package</code> in npx. This is maintained, but only for the <code>npx</code> 362executable.</li> 363<li>The <code>--ignore-existing</code> option is removed. Locally installed bins are 364always present in the executed process <code>PATH</code>.</li> 365<li>The <code>--npm</code> option is removed. <code>npx</code> will always use the <code>npm</code> it ships 366with.</li> 367<li>The <code>--node-arg</code> and <code>-n</code> options are removed.</li> 368<li>The <code>--always-spawn</code> option is redundant, and thus removed.</li> 369<li>The <code>--shell</code> option is replaced with <code>--script-shell</code>, but maintained 370in the <code>npx</code> executable for backwards compatibility.</li> 371</ul> 372<h3 id="a-note-on-caching">A note on caching</h3> 373<p>The npm cli utilizes its internal package cache when using the package 374name specified. You can use the following to change how and when the 375cli uses this cache. See <a href="../commands/npm-cache.html"><code>npm cache</code></a> for more on 376how the cache works.</p> 377<h4 id="prefer-online">prefer-online</h4> 378<p>Forces staleness checks for packages, making the cli look for updates 379immediately even if the package is already in the cache.</p> 380<h4 id="prefer-offline">prefer-offline</h4> 381<p>Bypasses staleness checks for packages. Missing data will still be 382requested from the server. To force full offline mode, use <code>offline</code>.</p> 383<h4 id="offline">offline</h4> 384<p>Forces full offline mode. Any packages not locally cached will result in 385an error.</p> 386<h4 id="workspace2">workspace</h4> 387<ul> 388<li>Default:</li> 389<li>Type: String (can be set multiple times)</li> 390</ul> 391<p>Enable running a command in the context of the configured workspaces of the 392current project while filtering by running only the workspaces defined by 393this configuration option.</p> 394<p>Valid values for the <code>workspace</code> config are either:</p> 395<ul> 396<li>Workspace names</li> 397<li>Path to a workspace directory</li> 398<li>Path to a parent workspace directory (will result to selecting all of the 399nested workspaces)</li> 400</ul> 401<p>This value is not exported to the environment for child processes.</p> 402<h4 id="workspaces2">workspaces</h4> 403<ul> 404<li>Alias: <code>--ws</code></li> 405<li>Type: Boolean</li> 406<li>Default: <code>false</code></li> 407</ul> 408<p>Run scripts in the context of all configured workspaces for the current 409project.</p> 410<h3 id="see-also">See Also</h3> 411<ul> 412<li><a href="../commands/npm-run-script.html">npm run-script</a></li> 413<li><a href="../using-npm/scripts.html">npm scripts</a></li> 414<li><a href="../commands/npm-test.html">npm test</a></li> 415<li><a href="../commands/npm-start.html">npm start</a></li> 416<li><a href="../commands/npm-restart.html">npm restart</a></li> 417<li><a href="../commands/npm-stop.html">npm stop</a></li> 418<li><a href="../commands/npm-config.html">npm config</a></li> 419<li><a href="../using-npm/workspaces.html">npm workspaces</a></li> 420<li><a href="../commands/npx.html">npx</a></li> 421</ul></div> 422 423<footer id="edit"> 424<a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-exec.md"> 425<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;"> 426<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> 427</svg> 428Edit this page on GitHub 429</a> 430</footer> 431</section> 432 433 434 435</body></html>