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<p><code>prepare</code> will not run during <code>--dry-run</code></p> 297<h4 id="npm-rebuild"><a href="../commands/npm-rebuild.html"><code>npm rebuild</code></a></h4> 298<ul> 299<li><code>preinstall</code></li> 300<li><code>install</code></li> 301<li><code>postinstall</code></li> 302<li><code>prepare</code></li> 303</ul> 304<p><code>prepare</code> is only run if the current directory is a symlink (e.g. with 305linked packages)</p> 306<h4 id="npm-restart"><a href="../commands/npm-restart.html"><code>npm restart</code></a></h4> 307<p>If there is a <code>restart</code> script defined, these events are run, otherwise 308<code>stop</code> and <code>start</code> are both run if present, including their <code>pre</code> and 309<code>post</code> iterations)</p> 310<ul> 311<li><code>prerestart</code></li> 312<li><code>restart</code></li> 313<li><code>postrestart</code></li> 314</ul> 315<h4 id="npm-run-user-defined"><a href="../commands/npm-run-script.html"><code>npm run <user defined></code></a></h4> 316<ul> 317<li><code>pre<user-defined></code></li> 318<li><code><user-defined></code></li> 319<li><code>post<user-defined></code></li> 320</ul> 321<h4 id="npm-start"><a href="../commands/npm-start.html"><code>npm start</code></a></h4> 322<ul> 323<li><code>prestart</code></li> 324<li><code>start</code></li> 325<li><code>poststart</code></li> 326</ul> 327<p>If there is a <code>server.js</code> file in the root of your package, then npm 328will default the <code>start</code> command to <code>node server.js</code>. <code>prestart</code> and 329<code>poststart</code> will still run in this case.</p> 330<h4 id="npm-stop"><a href="../commands/npm-stop.html"><code>npm stop</code></a></h4> 331<ul> 332<li><code>prestop</code></li> 333<li><code>stop</code></li> 334<li><code>poststop</code></li> 335</ul> 336<h4 id="npm-test"><a href="../commands/npm-test.html"><code>npm test</code></a></h4> 337<ul> 338<li><code>pretest</code></li> 339<li><code>test</code></li> 340<li><code>posttest</code></li> 341</ul> 342<h4 id="npm-version"><a href="../commands/npm-version.html"><code>npm version</code></a></h4> 343<ul> 344<li><code>preversion</code></li> 345<li><code>version</code></li> 346<li><code>postversion</code></li> 347</ul> 348<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> 349<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> 350<p>Reasons for a package removal include:</p> 351<ul> 352<li>a user directly uninstalled this package</li> 353<li>a user uninstalled a dependant package and so this dependency is being uninstalled</li> 354<li>a user uninstalled a dependant package but another package also depends on this version</li> 355<li>this version has been merged as a duplicate with another version</li> 356<li>etc.</li> 357</ul> 358<p>Due to the lack of necessary context, <code>uninstall</code> lifecycle scripts are not implemented and will not function.</p> 359<h3 id="user">User</h3> 360<p>When npm is run as root, scripts are always run with the effective uid 361and gid of the working directory owner.</p> 362<h3 id="environment">Environment</h3> 363<p>Package scripts run in an environment where many pieces of information 364are made available regarding the setup of npm and the current state of 365the process.</p> 366<h4 id="path">path</h4> 367<p>If you depend on modules that define executable scripts, like test 368suites, then those executables will be added to the <code>PATH</code> for 369executing the scripts. So, if your package.json has this:</p> 370<pre><code class="language-json">{ 371 "name" : "foo", 372 "dependencies" : { 373 "bar" : "0.1.x" 374 }, 375 "scripts": { 376 "start" : "bar ./test" 377 } 378} 379</code></pre> 380<p>then you could run <code>npm start</code> to execute the <code>bar</code> script, which is 381exported into the <code>node_modules/.bin</code> directory on <code>npm install</code>.</p> 382<h4 id="packagejson-vars">package.json vars</h4> 383<p>The package.json fields are tacked onto the <code>npm_package_</code> prefix. So, 384for instance, if you had <code>{"name":"foo", "version":"1.2.5"}</code> in your 385package.json file, then your package scripts would have the 386<code>npm_package_name</code> environment variable set to "foo", and the 387<code>npm_package_version</code> set to "1.2.5". You can access these variables 388in your code with <code>process.env.npm_package_name</code> and 389<code>process.env.npm_package_version</code>, and so on for other fields.</p> 390<p>See <a href="../configuring-npm/package-json.html"><code>package.json</code></a> for more on package configs.</p> 391<h4 id="current-lifecycle-event">current lifecycle event</h4> 392<p>Lastly, the <code>npm_lifecycle_event</code> environment variable is set to 393whichever stage of the cycle is being executed. So, you could have a 394single script used for different parts of the process which switches 395based on what's currently happening.</p> 396<p>Objects are flattened following this format, so if you had 397<code>{"scripts":{"install":"foo.js"}}</code> in your package.json, then you'd 398see this in the script:</p> 399<pre><code class="language-bash">process.env.npm_package_scripts_install === "foo.js" 400</code></pre> 401<h3 id="examples">Examples</h3> 402<p>For example, if your package.json contains this:</p> 403<pre><code class="language-json">{ 404 "scripts" : { 405 "install" : "scripts/install.js", 406 "postinstall" : "scripts/install.js", 407 "uninstall" : "scripts/uninstall.js" 408 } 409} 410</code></pre> 411<p>then <code>scripts/install.js</code> will be called for the install 412and post-install stages of the lifecycle, and <code>scripts/uninstall.js</code> 413will be called when the package is uninstalled. Since 414<code>scripts/install.js</code> is running for two different phases, it would 415be wise in this case to look at the <code>npm_lifecycle_event</code> environment 416variable.</p> 417<p>If you want to run a make command, you can do so. This works just 418fine:</p> 419<pre><code class="language-json">{ 420 "scripts" : { 421 "preinstall" : "./configure", 422 "install" : "make && make install", 423 "test" : "make test" 424 } 425} 426</code></pre> 427<h3 id="exiting">Exiting</h3> 428<p>Scripts are run by passing the line as a script argument to <code>sh</code>.</p> 429<p>If the script exits with a code other than 0, then this will abort the 430process.</p> 431<p>Note that these script files don't have to be Node.js or even 432JavaScript programs. They just have to be some kind of executable 433file.</p> 434<h3 id="best-practices">Best Practices</h3> 435<ul> 436<li>Don't exit with a non-zero error code unless you <em>really</em> mean it. 437Except for uninstall scripts, this will cause the npm action to 438fail, and potentially be rolled back. If the failure is minor or 439only will prevent some optional features, then it's better to just 440print a warning and exit successfully.</li> 441<li>Try not to use scripts to do what npm can do for you. Read through 442<a href="../configuring-npm/package-json.html"><code>package.json</code></a> to see all the things that you can specify and enable 443by simply describing your package appropriately. In general, this 444will lead to a more robust and consistent state.</li> 445<li>Inspect the env to determine where to put things. For instance, if 446the <code>npm_config_binroot</code> environment variable is set to <code>/home/user/bin</code>, then 447don't try to install executables into <code>/usr/local/bin</code>. The user 448probably set it up that way for a reason.</li> 449<li>Don't prefix your script commands with "sudo". If root permissions 450are required for some reason, then it'll fail with that error, and 451the user will sudo the npm command in question.</li> 452<li>Don't use <code>install</code>. Use a <code>.gyp</code> file for compilation, and <code>prepare</code> 453for anything else. You should almost never have to explicitly set a 454preinstall or install script. If you are doing this, please consider if 455there is another option. The only valid use of <code>install</code> or <code>preinstall</code> 456scripts is for compilation which must be done on the target architecture.</li> 457<li>Scripts are run from the root of the package folder, regardless of what the 458current working directory is when <code>npm</code> is invoked. If you want your 459script to use different behavior based on what subdirectory you're in, you 460can use the <code>INIT_CWD</code> environment variable, which holds the full path you 461were in when you ran <code>npm run</code>.</li> 462</ul> 463<h3 id="see-also">See Also</h3> 464<ul> 465<li><a href="../commands/npm-run-script.html">npm run-script</a></li> 466<li><a href="../configuring-npm/package-json.html">package.json</a></li> 467<li><a href="../using-npm/developers.html">npm developers</a></li> 468<li><a href="../commands/npm-install.html">npm install</a></li> 469</ul></div> 470 471<footer id="edit"> 472<a href="https://github.com/npm/cli/edit/latest/docs/content/using-npm/scripts.md"> 473<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;"> 474<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> 475</svg> 476Edit this page on GitHub 477</a> 478</footer> 479</section> 480 481 482 483</body></html>