npm-run-script.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <html><head>
  2. <title>npm-run-script</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-run-script">npm-run-script</h1>
  125. <span class="description">Run arbitrary package scripts</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="#workspaces-support">Workspaces support</a></li><ul><li><a href="#filtering-workspaces">Filtering workspaces</a></li></ul><li><a href="#configuration">Configuration</a></li><ul><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><li><a href="#if-present"><code>if-present</code></a></li><li><a href="#ignore-scripts"><code>ignore-scripts</code></a></li><li><a href="#script-shell"><code>script-shell</code></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 run-script &lt;command&gt; [--if-present] [--silent] [-- &lt;args&gt;]
  133. npm run-script &lt;command&gt; [--workspace=&lt;workspace-name&gt;]
  134. npm run-script &lt;command&gt; [--workspaces]
  135. aliases: run, rum, urn
  136. </code></pre>
  137. <h3 id="description">Description</h3>
  138. <p>This runs an arbitrary command from a package’s <code>"scripts"</code> object. If no
  139. <code>"command"</code> is provided, it will list the available scripts.</p>
  140. <p><code>run[-script]</code> is used by the test, start, restart, and stop commands, but
  141. can be called directly, as well. When the scripts in the package are
  142. printed out, they’re separated into lifecycle (test, start, restart) and
  143. directly-run scripts.</p>
  144. <p>Any positional arguments are passed to the specified script. Use <code>--</code> to
  145. pass <code>-</code>-prefixed flags and options which would otherwise be parsed by npm.</p>
  146. <p>For example:</p>
  147. <pre lang="bash"><code>npm run test -- --grep="pattern"
  148. </code></pre>
  149. <p>The arguments will only be passed to the script specified after <code>npm run</code>
  150. and not to any <code>pre</code> or <code>post</code> script.</p>
  151. <p>The <code>env</code> script is a special built-in command that can be used to list
  152. environment variables that will be available to the script at runtime. If an
  153. “env” command is defined in your package, it will take precedence over the
  154. built-in.</p>
  155. <p>In addition to the shell’s pre-existing <code>PATH</code>, <code>npm run</code> adds
  156. <code>node_modules/.bin</code> to the <code>PATH</code> provided to scripts. Any binaries
  157. provided by locally-installed dependencies can be used without the
  158. <code>node_modules/.bin</code> prefix. For example, if there is a <code>devDependency</code> on
  159. <code>tap</code> in your package, you should write:</p>
  160. <pre lang="bash"><code>"scripts": {"test": "tap test/*.js"}
  161. </code></pre>
  162. <p>instead of</p>
  163. <pre lang="bash"><code>"scripts": {"test": "node_modules/.bin/tap test/*.js"}
  164. </code></pre>
  165. <p>The actual shell your script is run within is platform dependent. By default,
  166. on Unix-like systems it is the <code>/bin/sh</code> command, on Windows it is
  167. <code>cmd.exe</code>.
  168. The actual shell referred to by <code>/bin/sh</code> also depends on the system.
  169. You can customize the shell with the <code>script-shell</code> configuration.</p>
  170. <p>Scripts are run from the root of the package folder, regardless of what the
  171. current working directory is when <code>npm run</code> is called. If you want your
  172. script to use different behavior based on what subdirectory you’re in, you
  173. can use the <code>INIT_CWD</code> environment variable, which holds the full path you
  174. were in when you ran <code>npm run</code>.</p>
  175. <p><code>npm run</code> sets the <code>NODE</code> environment variable to the <code>node</code> executable
  176. with which <code>npm</code> is executed.</p>
  177. <p>If you try to run a script without having a <code>node_modules</code> directory and it
  178. fails, you will be given a warning to run <code>npm install</code>, just in case you’ve
  179. forgotten.</p>
  180. <h3 id="workspaces-support">Workspaces support</h3>
  181. <p>You may use the <code>workspace</code> or <code>workspaces</code> configs in order to run an
  182. arbitrary command from a package’s <code>"scripts"</code> object in the context of the
  183. specified workspaces. If no <code>"command"</code> is provided, it will list the available
  184. scripts for each of these configured workspaces.</p>
  185. <p>Given a project with configured workspaces, e.g:</p>
  186. <pre><code>.
  187. +-- package.json
  188. `-- packages
  189. +-- a
  190. | `-- package.json
  191. +-- b
  192. | `-- package.json
  193. `-- c
  194. `-- package.json
  195. </code></pre>
  196. <p>Assuming the workspace configuration is properly set up at the root level
  197. <code>package.json</code> file. e.g:</p>
  198. <pre><code>{
  199. "workspaces": [ "./packages/*" ]
  200. }
  201. </code></pre>
  202. <p>And that each of the configured workspaces has a configured <code>test</code> script,
  203. we can run tests in all of them using the <code>workspaces</code> config:</p>
  204. <pre><code>npm test --workspaces
  205. </code></pre>
  206. <h4 id="filtering-workspaces">Filtering workspaces</h4>
  207. <p>It’s also possible to run a script in a single workspace using the <code>workspace</code>
  208. config along with a name or directory path:</p>
  209. <pre><code>npm test --workspace=a
  210. </code></pre>
  211. <p>The <code>workspace</code> config can also be specified multiple times in order to run a
  212. specific script in the context of multiple workspaces. When defining values for
  213. the <code>workspace</code> config in the command line, it also possible to use <code>-w</code> as a
  214. shorthand, e.g:</p>
  215. <pre><code>npm test -w a -w b
  216. </code></pre>
  217. <p>This last command will run <code>test</code> in both <code>./packages/a</code> and <code>./packages/b</code>
  218. packages.</p>
  219. <h3 id="configuration">Configuration</h3>
  220. <!-- raw HTML omitted -->
  221. <!-- raw HTML omitted -->
  222. <!-- raw HTML omitted -->
  223. <h4 id="workspace"><code>workspace</code></h4>
  224. <ul>
  225. <li>Default:</li>
  226. <li>Type: String (can be set multiple times)</li>
  227. </ul>
  228. <p>Enable running a command in the context of the configured workspaces of the
  229. current project while filtering by running only the workspaces defined by
  230. this configuration option.</p>
  231. <p>Valid values for the <code>workspace</code> config are either:</p>
  232. <ul>
  233. <li>Workspace names</li>
  234. <li>Path to a workspace directory</li>
  235. <li>Path to a parent workspace directory (will result in selecting all
  236. workspaces within that folder)</li>
  237. </ul>
  238. <p>When set for the <code>npm init</code> command, this may be set to the folder of a
  239. workspace which does not yet exist, to create the folder and set it up as a
  240. brand new workspace within the project.</p>
  241. <p>This value is not exported to the environment for child processes.</p>
  242. <!-- raw HTML omitted -->
  243. <!-- raw HTML omitted -->
  244. <h4 id="workspaces"><code>workspaces</code></h4>
  245. <ul>
  246. <li>Default: null</li>
  247. <li>Type: null or Boolean</li>
  248. </ul>
  249. <p>Set to true to run the command in the context of <strong>all</strong> configured
  250. workspaces.</p>
  251. <p>Explicitly setting this to false will cause commands like <code>install</code> to
  252. ignore workspaces altogether. When not set explicitly:</p>
  253. <ul>
  254. <li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
  255. will link workspaces into the <code>node_modules</code> folder. - Commands that do
  256. other things (test, exec, publish, etc.) will operate on the root project,
  257. <em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li>
  258. </ul>
  259. <p>This value is not exported to the environment for child processes.</p>
  260. <!-- raw HTML omitted -->
  261. <!-- raw HTML omitted -->
  262. <h4 id="include-workspace-root"><code>include-workspace-root</code></h4>
  263. <ul>
  264. <li>Default: false</li>
  265. <li>Type: Boolean</li>
  266. </ul>
  267. <p>Include the workspace root when workspaces are enabled for a command.</p>
  268. <p>When false, specifying individual workspaces via the <code>workspace</code> config, or
  269. all workspaces via the <code>workspaces</code> flag, will cause npm to operate only on
  270. the specified workspaces, and not on the root project.</p>
  271. <!-- raw HTML omitted -->
  272. <!-- raw HTML omitted -->
  273. <h4 id="if-present"><code>if-present</code></h4>
  274. <ul>
  275. <li>Default: false</li>
  276. <li>Type: Boolean</li>
  277. </ul>
  278. <p>If true, npm will not exit with an error code when <code>run-script</code> is invoked
  279. for a script that isn’t defined in the <code>scripts</code> section of <code>package.json</code>.
  280. This option can be used when it’s desirable to optionally run a script when
  281. it’s present and fail if the script fails. This is useful, for example, when
  282. running scripts that may only apply for some builds in an otherwise generic
  283. CI setup.</p>
  284. <!-- raw HTML omitted -->
  285. <!-- raw HTML omitted -->
  286. <h4 id="ignore-scripts"><code>ignore-scripts</code></h4>
  287. <ul>
  288. <li>Default: false</li>
  289. <li>Type: Boolean</li>
  290. </ul>
  291. <p>If true, npm does not run scripts specified in package.json files.</p>
  292. <p>Note that commands explicitly intended to run a particular script, such as
  293. <code>npm start</code>, <code>npm stop</code>, <code>npm restart</code>, <code>npm test</code>, and <code>npm run-script</code>
  294. will still run their intended script if <code>ignore-scripts</code> is set, but they
  295. will <em>not</em> run any pre- or post-scripts.</p>
  296. <!-- raw HTML omitted -->
  297. <!-- raw HTML omitted -->
  298. <h4 id="script-shell"><code>script-shell</code></h4>
  299. <ul>
  300. <li>Default: ‘/bin/sh’ on POSIX systems, ‘cmd.exe’ on Windows</li>
  301. <li>Type: null or String</li>
  302. </ul>
  303. <p>The shell to use for scripts run with the <code>npm exec</code>, <code>npm run</code> and <code>npm init &lt;pkg&gt;</code> commands.</p>
  304. <!-- raw HTML omitted -->
  305. <!-- raw HTML omitted -->
  306. <!-- raw HTML omitted -->
  307. <h3 id="see-also">See Also</h3>
  308. <ul>
  309. <li><a href="../using-npm/scripts.html">npm scripts</a></li>
  310. <li><a href="../commands/npm-test.html">npm test</a></li>
  311. <li><a href="../commands/npm-start.html">npm start</a></li>
  312. <li><a href="../commands/npm-restart.html">npm restart</a></li>
  313. <li><a href="../commands/npm-stop.html">npm stop</a></li>
  314. <li><a href="../commands/npm-config.html">npm config</a></li>
  315. <li><a href="../using-npm/workspaces.html">npm workspaces</a></li>
  316. </ul>
  317. </div>
  318. <footer id="edit">
  319. <a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-run-script.md">
  320. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  321. <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>
  322. </svg>
  323. Edit this page on GitHub
  324. </a>
  325. </footer>
  326. </section>
  327. </body></html>