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