npx.html 12 KB

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