1<!DOCTYPE html><html><head> 2<meta charset="utf-8"> 3<title>scripts</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="scripts">scripts</h1> 140<span class="description">How npm handles the "scripts" field</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="#description">Description</a></li><li><a href="#pre--post-scripts">Pre & Post Scripts</a></li><li><a href="#life-cycle-scripts">Life Cycle Scripts</a></li><ul><li><a href="#prepare-and-prepublish">Prepare and Prepublish</a></li><li><a href="#dependencies">Dependencies</a></li></ul><li><a href="#life-cycle-operation-order">Life Cycle Operation Order</a></li><ul><li><a href="#npm-cache-add"><a href="../commands/npm-cache.html"><code>npm cache add</code></a></a></li><li><a href="#npm-ci"><a href="../commands/npm-ci.html"><code>npm ci</code></a></a></li><li><a href="#npm-diff"><a href="../commands/npm-diff.html"><code>npm diff</code></a></a></li><li><a href="#npm-install"><a href="../commands/npm-install.html"><code>npm install</code></a></a></li><li><a href="#npm-pack"><a href="../commands/npm-pack.html"><code>npm pack</code></a></a></li><li><a href="#npm-publish"><a href="../commands/npm-publish.html"><code>npm publish</code></a></a></li><li><a href="#npm-rebuild"><a href="../commands/npm-rebuild.html"><code>npm rebuild</code></a></a></li><li><a href="#npm-restart"><a href="../commands/npm-restart.html"><code>npm restart</code></a></a></li><li><a href="#npm-run-user-defined"><a href="../commands/npm-run-script.html"><code>npm run <user defined></code></a></a></li><li><a href="#npm-start"><a href="../commands/npm-start.html"><code>npm start</code></a></a></li><li><a href="#npm-stop"><a href="../commands/npm-stop.html"><code>npm stop</code></a></a></li><li><a href="#npm-test"><a href="../commands/npm-test.html"><code>npm test</code></a></a></li><li><a href="#npm-version"><a href="../commands/npm-version.html"><code>npm version</code></a></a></li><li><a href="#a-note-on-a-lack-of-npm-uninstall-scripts">A Note on a lack of <a href="../commands/npm-uninstall.html"><code>npm uninstall</code></a> scripts</a></li></ul><li><a href="#user">User</a></li><li><a href="#environment">Environment</a></li><ul><li><a href="#path">path</a></li><li><a href="#packagejson-vars">package.json vars</a></li><li><a href="#current-lifecycle-event">current lifecycle event</a></li></ul><li><a href="#examples">Examples</a></li><li><a href="#exiting">Exiting</a></li><li><a href="#best-practices">Best Practices</a></li><li><a href="#see-also">See Also</a></li></ul></div> 146</section> 147 148<div id="_content"><h3 id="description">Description</h3> 149<p>The <code>"scripts"</code> property of your <code>package.json</code> file supports a number 150of built-in scripts and their preset life cycle events as well as 151arbitrary scripts. These all can be executed by running 152<code>npm run-script <stage></code> or <code>npm run <stage></code> for short. <em>Pre</em> and <em>post</em> 153commands with matching names will be run for those as well (e.g. <code>premyscript</code>, 154<code>myscript</code>, <code>postmyscript</code>). Scripts from dependencies can be run with 155<code>npm explore <pkg> -- npm run <stage></code>.</p> 156<h3 id="pre--post-scripts">Pre & Post Scripts</h3> 157<p>To create "pre" or "post" scripts for any scripts defined in the 158<code>"scripts"</code> section of the <code>package.json</code>, simply create another script 159<em>with a matching name</em> and add "pre" or "post" to the beginning of them.</p> 160<pre><code class="language-json">{ 161 "scripts": { 162 "precompress": "{{ executes BEFORE the `compress` script }}", 163 "compress": "{{ run command to compress files }}", 164 "postcompress": "{{ executes AFTER `compress` script }}" 165 } 166} 167</code></pre> 168<p>In this example <code>npm run compress</code> would execute these scripts as 169described.</p> 170<h3 id="life-cycle-scripts">Life Cycle Scripts</h3> 171<p>There are some special life cycle scripts that happen only in certain 172situations. These scripts happen in addition to the <code>pre<event></code>, <code>post<event></code>, and 173<code><event></code> scripts.</p> 174<ul> 175<li><code>prepare</code>, <code>prepublish</code>, <code>prepublishOnly</code>, <code>prepack</code>, <code>postpack</code>, <code>dependencies</code></li> 176</ul> 177<p><strong>prepare</strong> (since <code>npm@4.0.0</code>)</p> 178<ul> 179<li> 180<p>Runs BEFORE the package is packed, i.e. during <code>npm publish</code> 181and <code>npm pack</code></p> 182</li> 183<li> 184<p>Runs on local <code>npm install</code> without any arguments</p> 185</li> 186<li> 187<p>Runs AFTER <code>prepublish</code>, but BEFORE <code>prepublishOnly</code></p> 188</li> 189<li> 190<p>NOTE: If a package being installed through git contains a <code>prepare</code> 191script, its <code>dependencies</code> and <code>devDependencies</code> will be installed, and 192the prepare script will be run, before the package is packaged and 193installed.</p> 194</li> 195<li> 196<p>As of <code>npm@7</code> these scripts run in the background. 197To see the output, run with: <code>--foreground-scripts</code>.</p> 198</li> 199</ul> 200<p><strong>prepublish</strong> (DEPRECATED)</p> 201<ul> 202<li>Does not run during <code>npm publish</code>, but does run during <code>npm ci</code> 203and <code>npm install</code>. See below for more info.</li> 204</ul> 205<p><strong>prepublishOnly</strong></p> 206<ul> 207<li>Runs BEFORE the package is prepared and packed, ONLY on <code>npm publish</code>.</li> 208</ul> 209<p><strong>prepack</strong></p> 210<ul> 211<li>Runs BEFORE a tarball is packed (on "<code>npm pack</code>", "<code>npm publish</code>", and when installing a git dependency).</li> 212<li>NOTE: "<code>npm run pack</code>" is NOT the same as "<code>npm pack</code>". "<code>npm run pack</code>" is an arbitrary user defined script name, where as, "<code>npm pack</code>" is a CLI defined command.</li> 213</ul> 214<p><strong>postpack</strong></p> 215<ul> 216<li>Runs AFTER the tarball has been generated but before it is moved to its final destination (if at all, publish does not save the tarball locally)</li> 217</ul> 218<p><strong>dependencies</strong></p> 219<ul> 220<li>Runs AFTER any operations that modify the <code>node_modules</code> directory IF changes occurred.</li> 221<li>Does NOT run in global mode</li> 222</ul> 223<h4 id="prepare-and-prepublish">Prepare and Prepublish</h4> 224<p><strong>Deprecation Note: prepublish</strong></p> 225<p>Since <code>npm@1.1.71</code>, the npm CLI has run the <code>prepublish</code> script for both <code>npm publish</code> and <code>npm install</code>, because it's a convenient way to prepare a package for use (some common use cases are described in the section below). It has also turned out to be, in practice, <a href="https://github.com/npm/npm/issues/10074">very confusing</a>. As of <code>npm@4.0.0</code>, a new event has been introduced, <code>prepare</code>, that preserves this existing behavior. A <em>new</em> event, <code>prepublishOnly</code> has been added as a transitional strategy to allow users to avoid the confusing behavior of existing npm versions and only run on <code>npm publish</code> (for instance, running the tests one last time to ensure they're in good shape).</p> 226<p>See <a href="https://github.com/npm/npm/issues/10074">https://github.com/npm/npm/issues/10074</a> for a much lengthier justification, with further reading, for this change.</p> 227<p><strong>Use Cases</strong></p> 228<p>If you need to perform operations on your package before it is used, in a way that is not dependent on the operating system or architecture of the target system, use a <code>prepublish</code> script. This includes tasks such as:</p> 229<ul> 230<li>Compiling CoffeeScript source code into JavaScript.</li> 231<li>Creating minified versions of JavaScript source code.</li> 232<li>Fetching remote resources that your package will use.</li> 233</ul> 234<p>The advantage of doing these things at <code>prepublish</code> time is that they can be done once, in a single place, thus reducing complexity and variability. Additionally, this means that:</p> 235<ul> 236<li>You can depend on <code>coffee-script</code> as a <code>devDependency</code>, and thus 237your users don't need to have it installed.</li> 238<li>You don't need to include minifiers in your package, reducing 239the size for your users.</li> 240<li>You don't need to rely on your users having <code>curl</code> or <code>wget</code> or 241other system tools on the target machines.</li> 242</ul> 243<h4 id="dependencies">Dependencies</h4> 244<p>The <code>dependencies</code> script is run any time an <code>npm</code> command causes changes to the <code>node_modules</code> directory. It is run AFTER the changes have been applied and the <code>package.json</code> and <code>package-lock.json</code> files have been updated.</p> 245<h3 id="life-cycle-operation-order">Life Cycle Operation Order</h3> 246<h4 id="npm-cache-add"><a href="../commands/npm-cache.html"><code>npm cache add</code></a></h4> 247<ul> 248<li><code>prepare</code></li> 249</ul> 250<h4 id="npm-ci"><a href="../commands/npm-ci.html"><code>npm ci</code></a></h4> 251<ul> 252<li><code>preinstall</code></li> 253<li><code>install</code></li> 254<li><code>postinstall</code></li> 255<li><code>prepublish</code></li> 256<li><code>preprepare</code></li> 257<li><code>prepare</code></li> 258<li><code>postprepare</code></li> 259</ul> 260<p>These all run after the actual installation of modules into 261<code>node_modules</code>, in order, with no internal actions happening in between</p> 262<h4 id="npm-diff"><a href="../commands/npm-diff.html"><code>npm diff</code></a></h4> 263<ul> 264<li><code>prepare</code></li> 265</ul> 266<h4 id="npm-install"><a href="../commands/npm-install.html"><code>npm install</code></a></h4> 267<p>These also run when you run <code>npm install -g <pkg-name></code></p> 268<ul> 269<li><code>preinstall</code></li> 270<li><code>install</code></li> 271<li><code>postinstall</code></li> 272<li><code>prepublish</code></li> 273<li><code>preprepare</code></li> 274<li><code>prepare</code></li> 275<li><code>postprepare</code></li> 276</ul> 277<p>If there is a <code>binding.gyp</code> file in the root of your package and you 278haven't defined your own <code>install</code> or <code>preinstall</code> scripts, npm will 279default the <code>install</code> command to compile using node-gyp via <code>node-gyp rebuild</code></p> 280<p>These are run from the scripts of <code><pkg-name></code></p> 281<h4 id="npm-pack"><a href="../commands/npm-pack.html"><code>npm pack</code></a></h4> 282<ul> 283<li><code>prepack</code></li> 284<li><code>prepare</code></li> 285<li><code>postpack</code></li> 286</ul> 287<h4 id="npm-publish"><a href="../commands/npm-publish.html"><code>npm publish</code></a></h4> 288<ul> 289<li><code>prepublishOnly</code></li> 290<li><code>prepack</code></li> 291<li><code>prepare</code></li> 292<li><code>postpack</code></li> 293<li><code>publish</code></li> 294<li><code>postpublish</code></li> 295</ul> 296<h4 id="npm-rebuild"><a href="../commands/npm-rebuild.html"><code>npm rebuild</code></a></h4> 297<ul> 298<li><code>preinstall</code></li> 299<li><code>install</code></li> 300<li><code>postinstall</code></li> 301<li><code>prepare</code></li> 302</ul> 303<p><code>prepare</code> is only run if the current directory is a symlink (e.g. with 304linked packages)</p> 305<h4 id="npm-restart"><a href="../commands/npm-restart.html"><code>npm restart</code></a></h4> 306<p>If there is a <code>restart</code> script defined, these events are run, otherwise 307<code>stop</code> and <code>start</code> are both run if present, including their <code>pre</code> and 308<code>post</code> iterations)</p> 309<ul> 310<li><code>prerestart</code></li> 311<li><code>restart</code></li> 312<li><code>postrestart</code></li> 313</ul> 314<h4 id="npm-run-user-defined"><a href="../commands/npm-run-script.html"><code>npm run <user defined></code></a></h4> 315<ul> 316<li><code>pre<user-defined></code></li> 317<li><code><user-defined></code></li> 318<li><code>post<user-defined></code></li> 319</ul> 320<h4 id="npm-start"><a href="../commands/npm-start.html"><code>npm start</code></a></h4> 321<ul> 322<li><code>prestart</code></li> 323<li><code>start</code></li> 324<li><code>poststart</code></li> 325</ul> 326<p>If there is a <code>server.js</code> file in the root of your package, then npm 327will default the <code>start</code> command to <code>node server.js</code>. <code>prestart</code> and 328<code>poststart</code> will still run in this case.</p> 329<h4 id="npm-stop"><a href="../commands/npm-stop.html"><code>npm stop</code></a></h4> 330<ul> 331<li><code>prestop</code></li> 332<li><code>stop</code></li> 333<li><code>poststop</code></li> 334</ul> 335<h4 id="npm-test"><a href="../commands/npm-test.html"><code>npm test</code></a></h4> 336<ul> 337<li><code>pretest</code></li> 338<li><code>test</code></li> 339<li><code>posttest</code></li> 340</ul> 341<h4 id="npm-version"><a href="../commands/npm-version.html"><code>npm version</code></a></h4> 342<ul> 343<li><code>preversion</code></li> 344<li><code>version</code></li> 345<li><code>postversion</code></li> 346</ul> 347<h4 id="a-note-on-a-lack-of-npm-uninstall-scripts">A Note on a lack of <a href="../commands/npm-uninstall.html"><code>npm uninstall</code></a> scripts</h4> 348<p>While npm v6 had <code>uninstall</code> lifecycle scripts, npm v7 does not. Removal of a package can happen for a wide variety of reasons, and there's no clear way to currently give the script enough context to be useful.</p> 349<p>Reasons for a package removal include:</p> 350<ul> 351<li>a user directly uninstalled this package</li> 352<li>a user uninstalled a dependant package and so this dependency is being uninstalled</li> 353<li>a user uninstalled a dependant package but another package also depends on this version</li> 354<li>this version has been merged as a duplicate with another version</li> 355<li>etc.</li> 356</ul> 357<p>Due to the lack of necessary context, <code>uninstall</code> lifecycle scripts are not implemented and will not function.</p> 358<h3 id="user">User</h3> 359<p>When npm is run as root, scripts are always run with the effective uid 360and gid of the working directory owner.</p> 361<h3 id="environment">Environment</h3> 362<p>Package scripts run in an environment where many pieces of information 363are made available regarding the setup of npm and the current state of 364the process.</p> 365<h4 id="path">path</h4> 366<p>If you depend on modules that define executable scripts, like test 367suites, then those executables will be added to the <code>PATH</code> for 368executing the scripts. So, if your package.json has this:</p> 369<pre><code class="language-json">{ 370 "name" : "foo", 371 "dependencies" : { 372 "bar" : "0.1.x" 373 }, 374 "scripts": { 375 "start" : "bar ./test" 376 } 377} 378</code></pre> 379<p>then you could run <code>npm start</code> to execute the <code>bar</code> script, which is 380exported into the <code>node_modules/.bin</code> directory on <code>npm install</code>.</p> 381<h4 id="packagejson-vars">package.json vars</h4> 382<p>The package.json fields are tacked onto the <code>npm_package_</code> prefix. So, 383for instance, if you had <code>{"name":"foo", "version":"1.2.5"}</code> in your 384package.json file, then your package scripts would have the 385<code>npm_package_name</code> environment variable set to "foo", and the 386<code>npm_package_version</code> set to "1.2.5". You can access these variables 387in your code with <code>process.env.npm_package_name</code> and 388<code>process.env.npm_package_version</code>, and so on for other fields.</p> 389<p>See <a href="../configuring-npm/package-json.html"><code>package.json</code></a> for more on package configs.</p> 390<h4 id="current-lifecycle-event">current lifecycle event</h4> 391<p>Lastly, the <code>npm_lifecycle_event</code> environment variable is set to 392whichever stage of the cycle is being executed. So, you could have a 393single script used for different parts of the process which switches 394based on what's currently happening.</p> 395<p>Objects are flattened following this format, so if you had 396<code>{"scripts":{"install":"foo.js"}}</code> in your package.json, then you'd 397see this in the script:</p> 398<pre><code class="language-bash">process.env.npm_package_scripts_install === "foo.js" 399</code></pre> 400<h3 id="examples">Examples</h3> 401<p>For example, if your package.json contains this:</p> 402<pre><code class="language-json">{ 403 "scripts" : { 404 "install" : "scripts/install.js", 405 "postinstall" : "scripts/install.js" 406 } 407} 408</code></pre> 409<p>then <code>scripts/install.js</code> will be called for the install and post-install 410stages of the lifecycle. Since <code>scripts/install.js</code> is running for two 411different phases, it would be wise in this case to look at the 412<code>npm_lifecycle_event</code> environment variable.</p> 413<p>If you want to run a make command, you can do so. This works just 414fine:</p> 415<pre><code class="language-json">{ 416 "scripts" : { 417 "preinstall" : "./configure", 418 "install" : "make && make install", 419 "test" : "make test" 420 } 421} 422</code></pre> 423<h3 id="exiting">Exiting</h3> 424<p>Scripts are run by passing the line as a script argument to <code>sh</code>.</p> 425<p>If the script exits with a code other than 0, then this will abort the 426process.</p> 427<p>Note that these script files don't have to be Node.js or even 428JavaScript programs. They just have to be some kind of executable 429file.</p> 430<h3 id="best-practices">Best Practices</h3> 431<ul> 432<li>Don't exit with a non-zero error code unless you <em>really</em> mean it. 433If the failure is minor or only will prevent some optional features, then 434it's better to just print a warning and exit successfully.</li> 435<li>Try not to use scripts to do what npm can do for you. Read through 436<a href="../configuring-npm/package-json.html"><code>package.json</code></a> to see all the things that you can specify and enable 437by simply describing your package appropriately. In general, this 438will lead to a more robust and consistent state.</li> 439<li>Inspect the env to determine where to put things. For instance, if 440the <code>npm_config_binroot</code> environment variable is set to <code>/home/user/bin</code>, then 441don't try to install executables into <code>/usr/local/bin</code>. The user 442probably set it up that way for a reason.</li> 443<li>Don't prefix your script commands with "sudo". If root permissions 444are required for some reason, then it'll fail with that error, and 445the user will sudo the npm command in question.</li> 446<li>Don't use <code>install</code>. Use a <code>.gyp</code> file for compilation, and <code>prepare</code> 447for anything else. You should almost never have to explicitly set a 448preinstall or install script. If you are doing this, please consider if 449there is another option. The only valid use of <code>install</code> or <code>preinstall</code> 450scripts is for compilation which must be done on the target architecture.</li> 451<li>Scripts are run from the root of the package folder, regardless of what the 452current working directory is when <code>npm</code> is invoked. If you want your 453script to use different behavior based on what subdirectory you're in, you 454can use the <code>INIT_CWD</code> environment variable, which holds the full path you 455were in when you ran <code>npm run</code>.</li> 456</ul> 457<h3 id="see-also">See Also</h3> 458<ul> 459<li><a href="../commands/npm-run-script.html">npm run-script</a></li> 460<li><a href="../configuring-npm/package-json.html">package.json</a></li> 461<li><a href="../using-npm/developers.html">npm developers</a></li> 462<li><a href="../commands/npm-install.html">npm install</a></li> 463</ul></div> 464 465<footer id="edit"> 466<a href="https://github.com/npm/cli/edit/latest/docs/content/using-npm/scripts.md"> 467<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;"> 468<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> 469</svg> 470Edit this page on GitHub 471</a> 472</footer> 473</section> 474 475 476 477</body></html>