npm-exec.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <html><head>
  2. <title>npm-exec</title>
  3. <style>
  4. body {
  5. background-color: #ffffff;
  6. color: #24292e;
  7. margin: 0;
  8. line-height: 1.5;
  9. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  10. }
  11. #rainbar {
  12. height: 10px;
  13. background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
  14. }
  15. a {
  16. text-decoration: none;
  17. color: #0366d6;
  18. }
  19. a:hover {
  20. text-decoration: underline;
  21. }
  22. pre {
  23. margin: 1em 0px;
  24. padding: 1em;
  25. border: solid 1px #e1e4e8;
  26. border-radius: 6px;
  27. display: block;
  28. overflow: auto;
  29. white-space: pre;
  30. background-color: #f6f8fa;
  31. color: #393a34;
  32. }
  33. code {
  34. font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
  35. font-size: 85%;
  36. padding: 0.2em 0.4em;
  37. background-color: #f6f8fa;
  38. color: #393a34;
  39. }
  40. pre > code {
  41. padding: 0;
  42. background-color: inherit;
  43. color: inherit;
  44. }
  45. h1, h2, h3 {
  46. font-weight: 600;
  47. }
  48. #logobar {
  49. background-color: #333333;
  50. margin: 0 auto;
  51. padding: 1em 4em;
  52. }
  53. #logobar .logo {
  54. float: left;
  55. }
  56. #logobar .title {
  57. font-weight: 600;
  58. color: #dddddd;
  59. float: left;
  60. margin: 5px 0 0 1em;
  61. }
  62. #logobar:after {
  63. content: "";
  64. display: block;
  65. clear: both;
  66. }
  67. #content {
  68. margin: 0 auto;
  69. padding: 0 4em;
  70. }
  71. #table_of_contents > h2 {
  72. font-size: 1.17em;
  73. }
  74. #table_of_contents ul:first-child {
  75. border: solid 1px #e1e4e8;
  76. border-radius: 6px;
  77. padding: 1em;
  78. background-color: #f6f8fa;
  79. color: #393a34;
  80. }
  81. #table_of_contents ul {
  82. list-style-type: none;
  83. padding-left: 1.5em;
  84. }
  85. #table_of_contents li {
  86. font-size: 0.9em;
  87. }
  88. #table_of_contents li a {
  89. color: #000000;
  90. }
  91. header.title {
  92. border-bottom: solid 1px #e1e4e8;
  93. }
  94. header.title > h1 {
  95. margin-bottom: 0.25em;
  96. }
  97. header.title > .description {
  98. display: block;
  99. margin-bottom: 0.5em;
  100. line-height: 1;
  101. }
  102. footer#edit {
  103. border-top: solid 1px #e1e4e8;
  104. margin: 3em 0 4em 0;
  105. padding-top: 2em;
  106. }
  107. </style>
  108. </head>
  109. <body>
  110. <div id="banner">
  111. <div id="rainbar"></div>
  112. <div id="logobar">
  113. <svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
  114. <polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
  115. <polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
  116. </svg>
  117. <div class="title">
  118. npm command-line interface
  119. </div>
  120. </div>
  121. </div>
  122. <section id="content">
  123. <header class="title">
  124. <h1 id="npm-exec">npm-exec</h1>
  125. <span class="description">Run a command from a local or remote npm package</span>
  126. </header>
  127. <section id="table_of_contents">
  128. <h2 id="table-of-contents">Table of contents</h2>
  129. <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>
  130. </section>
  131. <div id="_content"><h3 id="synopsis">Synopsis</h3>
  132. <pre lang="bash"><code>npm exec -- &lt;pkg&gt;[@&lt;version&gt;] [args...]
  133. npm exec --package=&lt;pkg&gt;[@&lt;version&gt;] -- &lt;cmd&gt; [args...]
  134. npm exec -c '&lt;cmd&gt; [args...]'
  135. npm exec --package=foo -c '&lt;cmd&gt; [args...]'
  136. npm exec [--ws] [-w &lt;workspace-name] [args...]
  137. npx &lt;pkg&gt;[@&lt;specifier&gt;] [args...]
  138. npx -p &lt;pkg&gt;[@&lt;specifier&gt;] &lt;cmd&gt; [args...]
  139. npx -c '&lt;cmd&gt; [args...]'
  140. npx -p &lt;pkg&gt;[@&lt;specifier&gt;] -c '&lt;cmd&gt; [args...]'
  141. Run without --call or positional args to open interactive subshell
  142. alias: npm x, npx
  143. common options:
  144. --package=&lt;pkg&gt; (may be specified multiple times)
  145. -p is a shorthand for --package only when using npx executable
  146. -c &lt;cmd&gt; --call=&lt;cmd&gt; (may not be mixed with positional arguments)
  147. </code></pre>
  148. <h3 id="description">Description</h3>
  149. <p>This command allows you to run an arbitrary command from an npm package
  150. (either one installed locally, or fetched remotely), in a similar context
  151. as running it via <code>npm run</code>.</p>
  152. <p>Run without positional arguments or <code>--call</code>, this allows you to
  153. interactively run commands in the same sort of shell environment that
  154. <code>package.json</code> scripts are run. Interactive mode is not supported in CI
  155. environments when standard input is a TTY, to prevent hangs.</p>
  156. <p>Whatever packages are specified by the <code>--package</code> option will be
  157. provided in the <code>PATH</code> of the executed command, along with any locally
  158. installed package executables. The <code>--package</code> option may be
  159. specified multiple times, to execute the supplied command in an environment
  160. where all specified packages are available.</p>
  161. <p>If any requested packages are not present in the local project
  162. dependencies, then they are installed to a folder in the npm cache, which
  163. is added to the <code>PATH</code> environment variable in the executed process. A
  164. prompt is printed (which can be suppressed by providing either <code>--yes</code> or
  165. <code>--no</code>).</p>
  166. <p>Package names provided without a specifier will be matched with whatever
  167. version exists in the local project. Package names with a specifier will
  168. only be considered a match if they have the exact same name and version as
  169. the local dependency.</p>
  170. <p>If no <code>-c</code> or <code>--call</code> option is provided, then the positional arguments
  171. are used to generate the command string. If no <code>--package</code> options
  172. are provided, then npm will attempt to determine the executable name from
  173. the package specifier provided as the first positional argument according
  174. to the following heuristic:</p>
  175. <ul>
  176. <li>If the package has a single entry in its <code>bin</code> field in <code>package.json</code>,
  177. or if all entries are aliases of the same command, then that command
  178. will be used.</li>
  179. <li>If the package has multiple <code>bin</code> entries, and one of them matches the
  180. unscoped portion of the <code>name</code> field, then that command will be used.</li>
  181. <li>If this does not result in exactly one option (either because there are
  182. no bin entries, or none of them match the <code>name</code> of the package), then
  183. <code>npm exec</code> exits with an error.</li>
  184. </ul>
  185. <p>To run a binary <em>other than</em> the named binary, specify one or more
  186. <code>--package</code> options, which will prevent npm from inferring the package from
  187. the first command argument.</p>
  188. <h3 id="npx-vs-npm-exec"><code>npx</code> vs <code>npm exec</code></h3>
  189. <p>When run via the <code>npx</code> binary, all flags and options <em>must</em> be set prior to
  190. any positional arguments. When run via <code>npm exec</code>, a double-hyphen <code>--</code>
  191. flag can be used to suppress npm’s parsing of switches and options that
  192. should be sent to the executed command.</p>
  193. <p>For example:</p>
  194. <pre><code>$ npx foo@latest bar --package=@npmcli/foo
  195. </code></pre>
  196. <p>In this case, npm will resolve the <code>foo</code> package name, and run the
  197. following command:</p>
  198. <pre><code>$ foo bar --package=@npmcli/foo
  199. </code></pre>
  200. <p>Since the <code>--package</code> option comes <em>after</em> the positional arguments, it is
  201. treated as an argument to the executed command.</p>
  202. <p>In contrast, due to npm’s argument parsing logic, running this command is
  203. different:</p>
  204. <pre><code>$ npm exec foo@latest bar --package=@npmcli/foo
  205. </code></pre>
  206. <p>In this case, npm will parse the <code>--package</code> option first, resolving the
  207. <code>@npmcli/foo</code> package. Then, it will execute the following command in that
  208. context:</p>
  209. <pre><code>$ foo@latest bar
  210. </code></pre>
  211. <p>The double-hyphen character is recommended to explicitly tell npm to stop
  212. parsing command line options and switches. The following command would
  213. thus be equivalent to the <code>npx</code> command above:</p>
  214. <pre><code>$ npm exec -- foo@latest bar --package=@npmcli/foo
  215. </code></pre>
  216. <h3 id="configuration">Configuration</h3>
  217. <!-- raw HTML omitted -->
  218. <!-- raw HTML omitted -->
  219. <!-- raw HTML omitted -->
  220. <h4 id="package"><code>package</code></h4>
  221. <ul>
  222. <li>Default:</li>
  223. <li>Type: String (can be set multiple times)</li>
  224. </ul>
  225. <p>The package to install for <a href="../commands/npm-exec.html"><code>npm exec</code></a></p>
  226. <!-- raw HTML omitted -->
  227. <!-- raw HTML omitted -->
  228. <h4 id="call"><code>call</code></h4>
  229. <ul>
  230. <li>Default: “”</li>
  231. <li>Type: String</li>
  232. </ul>
  233. <p>Optional companion option for <code>npm exec</code>, <code>npx</code> that allows for specifying a
  234. custom command to be run along with the installed packages.</p>
  235. <pre lang="bash"><code>npm exec --package yo --package generator-node --call "yo node"
  236. </code></pre>
  237. <!-- raw HTML omitted -->
  238. <!-- raw HTML omitted -->
  239. <h4 id="workspace"><code>workspace</code></h4>
  240. <ul>
  241. <li>Default:</li>
  242. <li>Type: String (can be set multiple times)</li>
  243. </ul>
  244. <p>Enable running a command in the context of the configured workspaces of the
  245. current project while filtering by running only the workspaces defined by
  246. this configuration option.</p>
  247. <p>Valid values for the <code>workspace</code> config are either:</p>
  248. <ul>
  249. <li>Workspace names</li>
  250. <li>Path to a workspace directory</li>
  251. <li>Path to a parent workspace directory (will result in selecting all
  252. workspaces within that folder)</li>
  253. </ul>
  254. <p>When set for the <code>npm init</code> command, this may be set to the folder of a
  255. workspace which does not yet exist, to create the folder and set it up as a
  256. brand new workspace within the project.</p>
  257. <p>This value is not exported to the environment for child processes.</p>
  258. <!-- raw HTML omitted -->
  259. <!-- raw HTML omitted -->
  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
  266. workspaces.</p>
  267. <p>Explicitly setting this to false will cause commands like <code>install</code> to
  268. ignore workspaces altogether. When not set explicitly:</p>
  269. <ul>
  270. <li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
  271. will link workspaces into the <code>node_modules</code> folder. - Commands that do
  272. other 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. <!-- raw HTML omitted -->
  277. <!-- raw HTML omitted -->
  278. <h4 id="include-workspace-root"><code>include-workspace-root</code></h4>
  279. <ul>
  280. <li>Default: false</li>
  281. <li>Type: Boolean</li>
  282. </ul>
  283. <p>Include the workspace root when workspaces are enabled for a command.</p>
  284. <p>When false, specifying individual workspaces via the <code>workspace</code> config, or
  285. all workspaces via the <code>workspaces</code> flag, will cause npm to operate only on
  286. the specified workspaces, and not on the root project.</p>
  287. <!-- raw HTML omitted -->
  288. <!-- raw HTML omitted -->
  289. <!-- raw HTML omitted -->
  290. <h3 id="examples">Examples</h3>
  291. <p>Run the version of <code>tap</code> in the local dependencies, with the provided
  292. arguments:</p>
  293. <pre><code>$ npm exec -- tap --bail test/foo.js
  294. $ npx tap --bail test/foo.js
  295. </code></pre>
  296. <p>Run a command <em>other than</em> the command whose name matches the package name
  297. by specifying a <code>--package</code> option:</p>
  298. <pre><code>$ npm exec --package=foo -- bar --bar-argument
  299. # ~ or ~
  300. $ npx --package=foo bar --bar-argument
  301. </code></pre>
  302. <p>Run an arbitrary shell script, in the context of the current project:</p>
  303. <pre><code>$ npm x -c 'eslint &amp;&amp; say "hooray, lint passed"'
  304. $ npx -c 'eslint &amp;&amp; say "hooray, lint passed"'
  305. </code></pre>
  306. <h3 id="workspaces-support">Workspaces support</h3>
  307. <p>You may use the <code>workspace</code> or <code>workspaces</code> configs in order to run an
  308. arbitrary command from an npm package (either one installed locally, or fetched
  309. remotely) in the context of the specified workspaces.
  310. If no positional argument or <code>--call</code> option is provided, it will open an
  311. interactive subshell in the context of each of these configured workspaces one
  312. at a time.</p>
  313. <p>Given a project with configured workspaces, e.g:</p>
  314. <pre><code>.
  315. +-- package.json
  316. `-- packages
  317. +-- a
  318. | `-- package.json
  319. +-- b
  320. | `-- package.json
  321. `-- c
  322. `-- package.json
  323. </code></pre>
  324. <p>Assuming the workspace configuration is properly set up at the root level
  325. <code>package.json</code> file. e.g:</p>
  326. <pre><code>{
  327. "workspaces": [ "./packages/*" ]
  328. }
  329. </code></pre>
  330. <p>You can execute an arbitrary command from a package in the context of each of
  331. the configured workspaces when using the <code>workspaces</code> configuration options,
  332. in this example we’re using <strong>eslint</strong> to lint any js file found within each
  333. workspace folder:</p>
  334. <pre><code>npm exec --ws -- eslint ./*.js
  335. </code></pre>
  336. <h4 id="filtering-workspaces">Filtering workspaces</h4>
  337. <p>It’s also possible to execute a command in a single workspace using the
  338. <code>workspace</code> config along with a name or directory path:</p>
  339. <pre><code>npm exec --workspace=a -- eslint ./*.js
  340. </code></pre>
  341. <p>The <code>workspace</code> config can also be specified multiple times in order to run a
  342. specific script in the context of multiple workspaces. When defining values for
  343. the <code>workspace</code> config in the command line, it also possible to use <code>-w</code> as a
  344. shorthand, e.g:</p>
  345. <pre><code>npm exec -w a -w b -- eslint ./*.js
  346. </code></pre>
  347. <p>This last command will run the <code>eslint</code> command in both <code>./packages/a</code> and
  348. <code>./packages/b</code> folders.</p>
  349. <h3 id="compatibility-with-older-npx-versions">Compatibility with Older npx Versions</h3>
  350. <p>The <code>npx</code> binary was rewritten in npm v7.0.0, and the standalone <code>npx</code>
  351. package deprecated at that time. <code>npx</code> uses the <code>npm exec</code>
  352. command instead of a separate argument parser and install process, with
  353. some affordances to maintain backwards compatibility with the arguments it
  354. accepted in previous versions.</p>
  355. <p>This resulted in some shifts in its functionality:</p>
  356. <ul>
  357. <li>Any <code>npm</code> config value may be provided.</li>
  358. <li>To prevent security and user-experience problems from mistyping package
  359. names, <code>npx</code> prompts before installing anything. Suppress this
  360. prompt with the <code>-y</code> or <code>--yes</code> option.</li>
  361. <li>The <code>--no-install</code> option is deprecated, and will be converted to <code>--no</code>.</li>
  362. <li>Shell fallback functionality is removed, as it is not advisable.</li>
  363. <li>The <code>-p</code> argument is a shorthand for <code>--parseable</code> in npm, but shorthand
  364. for <code>--package</code> in npx. This is maintained, but only for the <code>npx</code>
  365. executable.</li>
  366. <li>The <code>--ignore-existing</code> option is removed. Locally installed bins are
  367. always present in the executed process <code>PATH</code>.</li>
  368. <li>The <code>--npm</code> option is removed. <code>npx</code> will always use the <code>npm</code> it ships
  369. with.</li>
  370. <li>The <code>--node-arg</code> and <code>-n</code> options are removed.</li>
  371. <li>The <code>--always-spawn</code> option is redundant, and thus removed.</li>
  372. <li>The <code>--shell</code> option is replaced with <code>--script-shell</code>, but maintained
  373. in the <code>npx</code> executable for backwards compatibility.</li>
  374. </ul>
  375. <h3 id="a-note-on-caching">A note on caching</h3>
  376. <p>The npm cli utilizes its internal package cache when using the package
  377. name specified. You can use the following to change how and when the
  378. cli uses this cache. See <a href="../commands/npm-cache.html"><code>npm cache</code></a> for more on
  379. how the cache works.</p>
  380. <h4 id="prefer-online">prefer-online</h4>
  381. <p>Forces staleness checks for packages, making the cli look for updates
  382. immediately even if the package is already in the cache.</p>
  383. <h4 id="prefer-offline">prefer-offline</h4>
  384. <p>Bypasses staleness checks for packages. Missing data will still be
  385. requested from the server. To force full offline mode, use <code>offline</code>.</p>
  386. <h4 id="offline">offline</h4>
  387. <p>Forces full offline mode. Any packages not locally cached will result in
  388. an error.</p>
  389. <h4 id="workspace2">workspace</h4>
  390. <ul>
  391. <li>Default:</li>
  392. <li>Type: String (can be set multiple times)</li>
  393. </ul>
  394. <p>Enable running a command in the context of the configured workspaces of the
  395. current project while filtering by running only the workspaces defined by
  396. this configuration option.</p>
  397. <p>Valid values for the <code>workspace</code> config are either:</p>
  398. <ul>
  399. <li>Workspace names</li>
  400. <li>Path to a workspace directory</li>
  401. <li>Path to a parent workspace directory (will result to selecting all of the
  402. nested workspaces)</li>
  403. </ul>
  404. <p>This value is not exported to the environment for child processes.</p>
  405. <h4 id="workspaces2">workspaces</h4>
  406. <ul>
  407. <li>Alias: <code>--ws</code></li>
  408. <li>Type: Boolean</li>
  409. <li>Default: <code>false</code></li>
  410. </ul>
  411. <p>Run scripts in the context of all configured workspaces for the current
  412. project.</p>
  413. <h3 id="see-also">See Also</h3>
  414. <ul>
  415. <li><a href="../commands/npm-run-script.html">npm run-script</a></li>
  416. <li><a href="../using-npm/scripts.html">npm scripts</a></li>
  417. <li><a href="../commands/npm-test.html">npm test</a></li>
  418. <li><a href="../commands/npm-start.html">npm start</a></li>
  419. <li><a href="../commands/npm-restart.html">npm restart</a></li>
  420. <li><a href="../commands/npm-stop.html">npm stop</a></li>
  421. <li><a href="../commands/npm-config.html">npm config</a></li>
  422. <li><a href="../using-npm/workspaces.html">npm workspaces</a></li>
  423. </ul>
  424. </div>
  425. <footer id="edit">
  426. <a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-exec.md">
  427. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  428. <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>
  429. </svg>
  430. Edit this page on GitHub
  431. </a>
  432. </footer>
  433. </section>
  434. </body></html>