11cb0ef41Sopenharmony_ci<!DOCTYPE html><html><head>
21cb0ef41Sopenharmony_ci<meta charset="utf-8">
31cb0ef41Sopenharmony_ci<title>scripts</title>
41cb0ef41Sopenharmony_ci<style>
51cb0ef41Sopenharmony_cibody {
61cb0ef41Sopenharmony_ci    background-color: #ffffff;
71cb0ef41Sopenharmony_ci    color: #24292e;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci    margin: 0;
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci    line-height: 1.5;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
141cb0ef41Sopenharmony_ci}
151cb0ef41Sopenharmony_ci#rainbar {
161cb0ef41Sopenharmony_ci    height: 10px;
171cb0ef41Sopenharmony_ci    background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cia {
211cb0ef41Sopenharmony_ci    text-decoration: none;
221cb0ef41Sopenharmony_ci    color: #0366d6;
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_cia:hover {
251cb0ef41Sopenharmony_ci    text-decoration: underline;
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cipre {
291cb0ef41Sopenharmony_ci    margin: 1em 0px;
301cb0ef41Sopenharmony_ci    padding: 1em;
311cb0ef41Sopenharmony_ci    border: solid 1px #e1e4e8;
321cb0ef41Sopenharmony_ci    border-radius: 6px;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    display: block;
351cb0ef41Sopenharmony_ci    overflow: auto;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    white-space: pre;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    background-color: #f6f8fa;
401cb0ef41Sopenharmony_ci    color: #393a34;
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_cicode {
431cb0ef41Sopenharmony_ci    font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
441cb0ef41Sopenharmony_ci    font-size: 85%;
451cb0ef41Sopenharmony_ci    padding: 0.2em 0.4em;
461cb0ef41Sopenharmony_ci    background-color: #f6f8fa;
471cb0ef41Sopenharmony_ci    color: #393a34;
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_cipre > code {
501cb0ef41Sopenharmony_ci    padding: 0;
511cb0ef41Sopenharmony_ci    background-color: inherit;
521cb0ef41Sopenharmony_ci    color: inherit;
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_cih1, h2, h3 {
551cb0ef41Sopenharmony_ci    font-weight: 600;
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci#logobar {
591cb0ef41Sopenharmony_ci    background-color: #333333;
601cb0ef41Sopenharmony_ci    margin: 0 auto;
611cb0ef41Sopenharmony_ci    padding: 1em 4em;
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci#logobar .logo {
641cb0ef41Sopenharmony_ci    float: left;
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci#logobar .title {
671cb0ef41Sopenharmony_ci    font-weight: 600;
681cb0ef41Sopenharmony_ci    color: #dddddd;
691cb0ef41Sopenharmony_ci    float: left;
701cb0ef41Sopenharmony_ci    margin: 5px 0 0 1em;
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci#logobar:after {
731cb0ef41Sopenharmony_ci    content: "";
741cb0ef41Sopenharmony_ci    display: block;
751cb0ef41Sopenharmony_ci    clear: both;
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci#content {
791cb0ef41Sopenharmony_ci    margin: 0 auto;
801cb0ef41Sopenharmony_ci    padding: 0 4em;
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci#table_of_contents > h2 {
841cb0ef41Sopenharmony_ci    font-size: 1.17em;
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci#table_of_contents ul:first-child {
871cb0ef41Sopenharmony_ci    border: solid 1px #e1e4e8;
881cb0ef41Sopenharmony_ci    border-radius: 6px;
891cb0ef41Sopenharmony_ci    padding: 1em;
901cb0ef41Sopenharmony_ci    background-color: #f6f8fa;
911cb0ef41Sopenharmony_ci    color: #393a34;
921cb0ef41Sopenharmony_ci}
931cb0ef41Sopenharmony_ci#table_of_contents ul {
941cb0ef41Sopenharmony_ci    list-style-type: none;
951cb0ef41Sopenharmony_ci    padding-left: 1.5em;
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci#table_of_contents li {
981cb0ef41Sopenharmony_ci    font-size: 0.9em;
991cb0ef41Sopenharmony_ci}
1001cb0ef41Sopenharmony_ci#table_of_contents li a {
1011cb0ef41Sopenharmony_ci    color: #000000;
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ciheader.title {
1051cb0ef41Sopenharmony_ci    border-bottom: solid 1px #e1e4e8;
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ciheader.title > h1 {
1081cb0ef41Sopenharmony_ci    margin-bottom: 0.25em;
1091cb0ef41Sopenharmony_ci}
1101cb0ef41Sopenharmony_ciheader.title > .description {
1111cb0ef41Sopenharmony_ci    display: block;
1121cb0ef41Sopenharmony_ci    margin-bottom: 0.5em;
1131cb0ef41Sopenharmony_ci    line-height: 1;
1141cb0ef41Sopenharmony_ci}
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_cifooter#edit {
1171cb0ef41Sopenharmony_ci    border-top: solid 1px #e1e4e8;
1181cb0ef41Sopenharmony_ci    margin: 3em 0 4em 0;
1191cb0ef41Sopenharmony_ci    padding-top: 2em;
1201cb0ef41Sopenharmony_ci}
1211cb0ef41Sopenharmony_ci</style>
1221cb0ef41Sopenharmony_ci</head>
1231cb0ef41Sopenharmony_ci<body>
1241cb0ef41Sopenharmony_ci<div id="banner">
1251cb0ef41Sopenharmony_ci<div id="rainbar"></div>
1261cb0ef41Sopenharmony_ci<div id="logobar">
1271cb0ef41Sopenharmony_ci<svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
1281cb0ef41Sopenharmony_ci<polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
1291cb0ef41Sopenharmony_ci<polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
1301cb0ef41Sopenharmony_ci</svg>
1311cb0ef41Sopenharmony_ci<div class="title">
1321cb0ef41Sopenharmony_cinpm command-line interface
1331cb0ef41Sopenharmony_ci</div>
1341cb0ef41Sopenharmony_ci</div>
1351cb0ef41Sopenharmony_ci</div>
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci<section id="content">
1381cb0ef41Sopenharmony_ci<header class="title">
1391cb0ef41Sopenharmony_ci<h1 id="scripts">scripts</h1>
1401cb0ef41Sopenharmony_ci<span class="description">How npm handles the "scripts" field</span>
1411cb0ef41Sopenharmony_ci</header>
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci<section id="table_of_contents">
1441cb0ef41Sopenharmony_ci<h2 id="table-of-contents">Table of contents</h2>
1451cb0ef41Sopenharmony_ci<div id="_table_of_contents"><ul><li><a href="#description">Description</a></li><li><a href="#pre--post-scripts">Pre &amp; 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 &lt;user defined&gt;</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>
1461cb0ef41Sopenharmony_ci</section>
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci<div id="_content"><h3 id="description">Description</h3>
1491cb0ef41Sopenharmony_ci<p>The <code>"scripts"</code> property of your <code>package.json</code> file supports a number
1501cb0ef41Sopenharmony_ciof built-in scripts and their preset life cycle events as well as
1511cb0ef41Sopenharmony_ciarbitrary scripts. These all can be executed by running
1521cb0ef41Sopenharmony_ci<code>npm run-script &lt;stage&gt;</code> or <code>npm run &lt;stage&gt;</code> for short. <em>Pre</em> and <em>post</em>
1531cb0ef41Sopenharmony_cicommands with matching names will be run for those as well (e.g. <code>premyscript</code>,
1541cb0ef41Sopenharmony_ci<code>myscript</code>, <code>postmyscript</code>). Scripts from dependencies can be run with
1551cb0ef41Sopenharmony_ci<code>npm explore &lt;pkg&gt; -- npm run &lt;stage&gt;</code>.</p>
1561cb0ef41Sopenharmony_ci<h3 id="pre--post-scripts">Pre &amp; Post Scripts</h3>
1571cb0ef41Sopenharmony_ci<p>To create "pre" or "post" scripts for any scripts defined in the
1581cb0ef41Sopenharmony_ci<code>"scripts"</code> section of the <code>package.json</code>, simply create another script
1591cb0ef41Sopenharmony_ci<em>with a matching name</em> and add "pre" or "post" to the beginning of them.</p>
1601cb0ef41Sopenharmony_ci<pre><code class="language-json">{
1611cb0ef41Sopenharmony_ci  "scripts": {
1621cb0ef41Sopenharmony_ci    "precompress": "{{ executes BEFORE the `compress` script }}",
1631cb0ef41Sopenharmony_ci    "compress": "{{ run command to compress files }}",
1641cb0ef41Sopenharmony_ci    "postcompress": "{{ executes AFTER `compress` script }}"
1651cb0ef41Sopenharmony_ci  }
1661cb0ef41Sopenharmony_ci}
1671cb0ef41Sopenharmony_ci</code></pre>
1681cb0ef41Sopenharmony_ci<p>In this example <code>npm run compress</code> would execute these scripts as
1691cb0ef41Sopenharmony_cidescribed.</p>
1701cb0ef41Sopenharmony_ci<h3 id="life-cycle-scripts">Life Cycle Scripts</h3>
1711cb0ef41Sopenharmony_ci<p>There are some special life cycle scripts that happen only in certain
1721cb0ef41Sopenharmony_cisituations. These scripts happen in addition to the <code>pre&lt;event&gt;</code>, <code>post&lt;event&gt;</code>, and
1731cb0ef41Sopenharmony_ci<code>&lt;event&gt;</code> scripts.</p>
1741cb0ef41Sopenharmony_ci<ul>
1751cb0ef41Sopenharmony_ci<li><code>prepare</code>, <code>prepublish</code>, <code>prepublishOnly</code>, <code>prepack</code>, <code>postpack</code>, <code>dependencies</code></li>
1761cb0ef41Sopenharmony_ci</ul>
1771cb0ef41Sopenharmony_ci<p><strong>prepare</strong> (since <code>npm@4.0.0</code>)</p>
1781cb0ef41Sopenharmony_ci<ul>
1791cb0ef41Sopenharmony_ci<li>
1801cb0ef41Sopenharmony_ci<p>Runs BEFORE the package is packed, i.e. during <code>npm publish</code>
1811cb0ef41Sopenharmony_ciand <code>npm pack</code></p>
1821cb0ef41Sopenharmony_ci</li>
1831cb0ef41Sopenharmony_ci<li>
1841cb0ef41Sopenharmony_ci<p>Runs on local <code>npm install</code> without any arguments</p>
1851cb0ef41Sopenharmony_ci</li>
1861cb0ef41Sopenharmony_ci<li>
1871cb0ef41Sopenharmony_ci<p>Runs AFTER <code>prepublish</code>, but BEFORE <code>prepublishOnly</code></p>
1881cb0ef41Sopenharmony_ci</li>
1891cb0ef41Sopenharmony_ci<li>
1901cb0ef41Sopenharmony_ci<p>NOTE: If a package being installed through git contains a <code>prepare</code>
1911cb0ef41Sopenharmony_ciscript, its <code>dependencies</code> and <code>devDependencies</code> will be installed, and
1921cb0ef41Sopenharmony_cithe prepare script will be run, before the package is packaged and
1931cb0ef41Sopenharmony_ciinstalled.</p>
1941cb0ef41Sopenharmony_ci</li>
1951cb0ef41Sopenharmony_ci<li>
1961cb0ef41Sopenharmony_ci<p>As of <code>npm@7</code> these scripts run in the background.
1971cb0ef41Sopenharmony_ciTo see the output, run with: <code>--foreground-scripts</code>.</p>
1981cb0ef41Sopenharmony_ci</li>
1991cb0ef41Sopenharmony_ci</ul>
2001cb0ef41Sopenharmony_ci<p><strong>prepublish</strong> (DEPRECATED)</p>
2011cb0ef41Sopenharmony_ci<ul>
2021cb0ef41Sopenharmony_ci<li>Does not run during <code>npm publish</code>, but does run during <code>npm ci</code>
2031cb0ef41Sopenharmony_ciand <code>npm install</code>. See below for more info.</li>
2041cb0ef41Sopenharmony_ci</ul>
2051cb0ef41Sopenharmony_ci<p><strong>prepublishOnly</strong></p>
2061cb0ef41Sopenharmony_ci<ul>
2071cb0ef41Sopenharmony_ci<li>Runs BEFORE the package is prepared and packed, ONLY on <code>npm publish</code>.</li>
2081cb0ef41Sopenharmony_ci</ul>
2091cb0ef41Sopenharmony_ci<p><strong>prepack</strong></p>
2101cb0ef41Sopenharmony_ci<ul>
2111cb0ef41Sopenharmony_ci<li>Runs BEFORE a tarball is packed (on "<code>npm pack</code>", "<code>npm publish</code>", and when installing a git dependency).</li>
2121cb0ef41Sopenharmony_ci<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>
2131cb0ef41Sopenharmony_ci</ul>
2141cb0ef41Sopenharmony_ci<p><strong>postpack</strong></p>
2151cb0ef41Sopenharmony_ci<ul>
2161cb0ef41Sopenharmony_ci<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>
2171cb0ef41Sopenharmony_ci</ul>
2181cb0ef41Sopenharmony_ci<p><strong>dependencies</strong></p>
2191cb0ef41Sopenharmony_ci<ul>
2201cb0ef41Sopenharmony_ci<li>Runs AFTER any operations that modify the <code>node_modules</code> directory IF changes occurred.</li>
2211cb0ef41Sopenharmony_ci<li>Does NOT run in global mode</li>
2221cb0ef41Sopenharmony_ci</ul>
2231cb0ef41Sopenharmony_ci<h4 id="prepare-and-prepublish">Prepare and Prepublish</h4>
2241cb0ef41Sopenharmony_ci<p><strong>Deprecation Note: prepublish</strong></p>
2251cb0ef41Sopenharmony_ci<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>
2261cb0ef41Sopenharmony_ci<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>
2271cb0ef41Sopenharmony_ci<p><strong>Use Cases</strong></p>
2281cb0ef41Sopenharmony_ci<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>
2291cb0ef41Sopenharmony_ci<ul>
2301cb0ef41Sopenharmony_ci<li>Compiling CoffeeScript source code into JavaScript.</li>
2311cb0ef41Sopenharmony_ci<li>Creating minified versions of JavaScript source code.</li>
2321cb0ef41Sopenharmony_ci<li>Fetching remote resources that your package will use.</li>
2331cb0ef41Sopenharmony_ci</ul>
2341cb0ef41Sopenharmony_ci<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>
2351cb0ef41Sopenharmony_ci<ul>
2361cb0ef41Sopenharmony_ci<li>You can depend on <code>coffee-script</code> as a <code>devDependency</code>, and thus
2371cb0ef41Sopenharmony_ciyour users don't need to have it installed.</li>
2381cb0ef41Sopenharmony_ci<li>You don't need to include minifiers in your package, reducing
2391cb0ef41Sopenharmony_cithe size for your users.</li>
2401cb0ef41Sopenharmony_ci<li>You don't need to rely on your users having <code>curl</code> or <code>wget</code> or
2411cb0ef41Sopenharmony_ciother system tools on the target machines.</li>
2421cb0ef41Sopenharmony_ci</ul>
2431cb0ef41Sopenharmony_ci<h4 id="dependencies">Dependencies</h4>
2441cb0ef41Sopenharmony_ci<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>
2451cb0ef41Sopenharmony_ci<h3 id="life-cycle-operation-order">Life Cycle Operation Order</h3>
2461cb0ef41Sopenharmony_ci<h4 id="npm-cache-add"><a href="../commands/npm-cache.html"><code>npm cache add</code></a></h4>
2471cb0ef41Sopenharmony_ci<ul>
2481cb0ef41Sopenharmony_ci<li><code>prepare</code></li>
2491cb0ef41Sopenharmony_ci</ul>
2501cb0ef41Sopenharmony_ci<h4 id="npm-ci"><a href="../commands/npm-ci.html"><code>npm ci</code></a></h4>
2511cb0ef41Sopenharmony_ci<ul>
2521cb0ef41Sopenharmony_ci<li><code>preinstall</code></li>
2531cb0ef41Sopenharmony_ci<li><code>install</code></li>
2541cb0ef41Sopenharmony_ci<li><code>postinstall</code></li>
2551cb0ef41Sopenharmony_ci<li><code>prepublish</code></li>
2561cb0ef41Sopenharmony_ci<li><code>preprepare</code></li>
2571cb0ef41Sopenharmony_ci<li><code>prepare</code></li>
2581cb0ef41Sopenharmony_ci<li><code>postprepare</code></li>
2591cb0ef41Sopenharmony_ci</ul>
2601cb0ef41Sopenharmony_ci<p>These all run after the actual installation of modules into
2611cb0ef41Sopenharmony_ci<code>node_modules</code>, in order, with no internal actions happening in between</p>
2621cb0ef41Sopenharmony_ci<h4 id="npm-diff"><a href="../commands/npm-diff.html"><code>npm diff</code></a></h4>
2631cb0ef41Sopenharmony_ci<ul>
2641cb0ef41Sopenharmony_ci<li><code>prepare</code></li>
2651cb0ef41Sopenharmony_ci</ul>
2661cb0ef41Sopenharmony_ci<h4 id="npm-install"><a href="../commands/npm-install.html"><code>npm install</code></a></h4>
2671cb0ef41Sopenharmony_ci<p>These also run when you run <code>npm install -g &lt;pkg-name&gt;</code></p>
2681cb0ef41Sopenharmony_ci<ul>
2691cb0ef41Sopenharmony_ci<li><code>preinstall</code></li>
2701cb0ef41Sopenharmony_ci<li><code>install</code></li>
2711cb0ef41Sopenharmony_ci<li><code>postinstall</code></li>
2721cb0ef41Sopenharmony_ci<li><code>prepublish</code></li>
2731cb0ef41Sopenharmony_ci<li><code>preprepare</code></li>
2741cb0ef41Sopenharmony_ci<li><code>prepare</code></li>
2751cb0ef41Sopenharmony_ci<li><code>postprepare</code></li>
2761cb0ef41Sopenharmony_ci</ul>
2771cb0ef41Sopenharmony_ci<p>If there is a <code>binding.gyp</code> file in the root of your package and you
2781cb0ef41Sopenharmony_cihaven't defined your own <code>install</code> or <code>preinstall</code> scripts, npm will
2791cb0ef41Sopenharmony_cidefault the <code>install</code> command to compile using node-gyp via <code>node-gyp rebuild</code></p>
2801cb0ef41Sopenharmony_ci<p>These are run from the scripts of <code>&lt;pkg-name&gt;</code></p>
2811cb0ef41Sopenharmony_ci<h4 id="npm-pack"><a href="../commands/npm-pack.html"><code>npm pack</code></a></h4>
2821cb0ef41Sopenharmony_ci<ul>
2831cb0ef41Sopenharmony_ci<li><code>prepack</code></li>
2841cb0ef41Sopenharmony_ci<li><code>prepare</code></li>
2851cb0ef41Sopenharmony_ci<li><code>postpack</code></li>
2861cb0ef41Sopenharmony_ci</ul>
2871cb0ef41Sopenharmony_ci<h4 id="npm-publish"><a href="../commands/npm-publish.html"><code>npm publish</code></a></h4>
2881cb0ef41Sopenharmony_ci<ul>
2891cb0ef41Sopenharmony_ci<li><code>prepublishOnly</code></li>
2901cb0ef41Sopenharmony_ci<li><code>prepack</code></li>
2911cb0ef41Sopenharmony_ci<li><code>prepare</code></li>
2921cb0ef41Sopenharmony_ci<li><code>postpack</code></li>
2931cb0ef41Sopenharmony_ci<li><code>publish</code></li>
2941cb0ef41Sopenharmony_ci<li><code>postpublish</code></li>
2951cb0ef41Sopenharmony_ci</ul>
2961cb0ef41Sopenharmony_ci<h4 id="npm-rebuild"><a href="../commands/npm-rebuild.html"><code>npm rebuild</code></a></h4>
2971cb0ef41Sopenharmony_ci<ul>
2981cb0ef41Sopenharmony_ci<li><code>preinstall</code></li>
2991cb0ef41Sopenharmony_ci<li><code>install</code></li>
3001cb0ef41Sopenharmony_ci<li><code>postinstall</code></li>
3011cb0ef41Sopenharmony_ci<li><code>prepare</code></li>
3021cb0ef41Sopenharmony_ci</ul>
3031cb0ef41Sopenharmony_ci<p><code>prepare</code> is only run if the current directory is a symlink (e.g. with
3041cb0ef41Sopenharmony_cilinked packages)</p>
3051cb0ef41Sopenharmony_ci<h4 id="npm-restart"><a href="../commands/npm-restart.html"><code>npm restart</code></a></h4>
3061cb0ef41Sopenharmony_ci<p>If there is a <code>restart</code> script defined, these events are run, otherwise
3071cb0ef41Sopenharmony_ci<code>stop</code> and <code>start</code> are both run if present, including their <code>pre</code> and
3081cb0ef41Sopenharmony_ci<code>post</code> iterations)</p>
3091cb0ef41Sopenharmony_ci<ul>
3101cb0ef41Sopenharmony_ci<li><code>prerestart</code></li>
3111cb0ef41Sopenharmony_ci<li><code>restart</code></li>
3121cb0ef41Sopenharmony_ci<li><code>postrestart</code></li>
3131cb0ef41Sopenharmony_ci</ul>
3141cb0ef41Sopenharmony_ci<h4 id="npm-run-user-defined"><a href="../commands/npm-run-script.html"><code>npm run &lt;user defined&gt;</code></a></h4>
3151cb0ef41Sopenharmony_ci<ul>
3161cb0ef41Sopenharmony_ci<li><code>pre&lt;user-defined&gt;</code></li>
3171cb0ef41Sopenharmony_ci<li><code>&lt;user-defined&gt;</code></li>
3181cb0ef41Sopenharmony_ci<li><code>post&lt;user-defined&gt;</code></li>
3191cb0ef41Sopenharmony_ci</ul>
3201cb0ef41Sopenharmony_ci<h4 id="npm-start"><a href="../commands/npm-start.html"><code>npm start</code></a></h4>
3211cb0ef41Sopenharmony_ci<ul>
3221cb0ef41Sopenharmony_ci<li><code>prestart</code></li>
3231cb0ef41Sopenharmony_ci<li><code>start</code></li>
3241cb0ef41Sopenharmony_ci<li><code>poststart</code></li>
3251cb0ef41Sopenharmony_ci</ul>
3261cb0ef41Sopenharmony_ci<p>If there is a <code>server.js</code> file in the root of your package, then npm
3271cb0ef41Sopenharmony_ciwill default the <code>start</code> command to <code>node server.js</code>.  <code>prestart</code> and
3281cb0ef41Sopenharmony_ci<code>poststart</code> will still run in this case.</p>
3291cb0ef41Sopenharmony_ci<h4 id="npm-stop"><a href="../commands/npm-stop.html"><code>npm stop</code></a></h4>
3301cb0ef41Sopenharmony_ci<ul>
3311cb0ef41Sopenharmony_ci<li><code>prestop</code></li>
3321cb0ef41Sopenharmony_ci<li><code>stop</code></li>
3331cb0ef41Sopenharmony_ci<li><code>poststop</code></li>
3341cb0ef41Sopenharmony_ci</ul>
3351cb0ef41Sopenharmony_ci<h4 id="npm-test"><a href="../commands/npm-test.html"><code>npm test</code></a></h4>
3361cb0ef41Sopenharmony_ci<ul>
3371cb0ef41Sopenharmony_ci<li><code>pretest</code></li>
3381cb0ef41Sopenharmony_ci<li><code>test</code></li>
3391cb0ef41Sopenharmony_ci<li><code>posttest</code></li>
3401cb0ef41Sopenharmony_ci</ul>
3411cb0ef41Sopenharmony_ci<h4 id="npm-version"><a href="../commands/npm-version.html"><code>npm version</code></a></h4>
3421cb0ef41Sopenharmony_ci<ul>
3431cb0ef41Sopenharmony_ci<li><code>preversion</code></li>
3441cb0ef41Sopenharmony_ci<li><code>version</code></li>
3451cb0ef41Sopenharmony_ci<li><code>postversion</code></li>
3461cb0ef41Sopenharmony_ci</ul>
3471cb0ef41Sopenharmony_ci<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>
3481cb0ef41Sopenharmony_ci<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>
3491cb0ef41Sopenharmony_ci<p>Reasons for a package removal include:</p>
3501cb0ef41Sopenharmony_ci<ul>
3511cb0ef41Sopenharmony_ci<li>a user directly uninstalled this package</li>
3521cb0ef41Sopenharmony_ci<li>a user uninstalled a dependant package and so this dependency is being uninstalled</li>
3531cb0ef41Sopenharmony_ci<li>a user uninstalled a dependant package but another package also depends on this version</li>
3541cb0ef41Sopenharmony_ci<li>this version has been merged as a duplicate with another version</li>
3551cb0ef41Sopenharmony_ci<li>etc.</li>
3561cb0ef41Sopenharmony_ci</ul>
3571cb0ef41Sopenharmony_ci<p>Due to the lack of necessary context, <code>uninstall</code> lifecycle scripts are not implemented and will not function.</p>
3581cb0ef41Sopenharmony_ci<h3 id="user">User</h3>
3591cb0ef41Sopenharmony_ci<p>When npm is run as root, scripts are always run with the effective uid
3601cb0ef41Sopenharmony_ciand gid of the working directory owner.</p>
3611cb0ef41Sopenharmony_ci<h3 id="environment">Environment</h3>
3621cb0ef41Sopenharmony_ci<p>Package scripts run in an environment where many pieces of information
3631cb0ef41Sopenharmony_ciare made available regarding the setup of npm and the current state of
3641cb0ef41Sopenharmony_cithe process.</p>
3651cb0ef41Sopenharmony_ci<h4 id="path">path</h4>
3661cb0ef41Sopenharmony_ci<p>If you depend on modules that define executable scripts, like test
3671cb0ef41Sopenharmony_cisuites, then those executables will be added to the <code>PATH</code> for
3681cb0ef41Sopenharmony_ciexecuting the scripts.  So, if your package.json has this:</p>
3691cb0ef41Sopenharmony_ci<pre><code class="language-json">{
3701cb0ef41Sopenharmony_ci  "name" : "foo",
3711cb0ef41Sopenharmony_ci  "dependencies" : {
3721cb0ef41Sopenharmony_ci    "bar" : "0.1.x"
3731cb0ef41Sopenharmony_ci  },
3741cb0ef41Sopenharmony_ci  "scripts": {
3751cb0ef41Sopenharmony_ci    "start" : "bar ./test"
3761cb0ef41Sopenharmony_ci  }
3771cb0ef41Sopenharmony_ci}
3781cb0ef41Sopenharmony_ci</code></pre>
3791cb0ef41Sopenharmony_ci<p>then you could run <code>npm start</code> to execute the <code>bar</code> script, which is
3801cb0ef41Sopenharmony_ciexported into the <code>node_modules/.bin</code> directory on <code>npm install</code>.</p>
3811cb0ef41Sopenharmony_ci<h4 id="packagejson-vars">package.json vars</h4>
3821cb0ef41Sopenharmony_ci<p>The package.json fields are tacked onto the <code>npm_package_</code> prefix. So,
3831cb0ef41Sopenharmony_cifor instance, if you had <code>{"name":"foo", "version":"1.2.5"}</code> in your
3841cb0ef41Sopenharmony_cipackage.json file, then your package scripts would have the
3851cb0ef41Sopenharmony_ci<code>npm_package_name</code> environment variable set to "foo", and the
3861cb0ef41Sopenharmony_ci<code>npm_package_version</code> set to "1.2.5".  You can access these variables
3871cb0ef41Sopenharmony_ciin your code with <code>process.env.npm_package_name</code> and
3881cb0ef41Sopenharmony_ci<code>process.env.npm_package_version</code>, and so on for other fields.</p>
3891cb0ef41Sopenharmony_ci<p>See <a href="../configuring-npm/package-json.html"><code>package.json</code></a> for more on package configs.</p>
3901cb0ef41Sopenharmony_ci<h4 id="current-lifecycle-event">current lifecycle event</h4>
3911cb0ef41Sopenharmony_ci<p>Lastly, the <code>npm_lifecycle_event</code> environment variable is set to
3921cb0ef41Sopenharmony_ciwhichever stage of the cycle is being executed. So, you could have a
3931cb0ef41Sopenharmony_cisingle script used for different parts of the process which switches
3941cb0ef41Sopenharmony_cibased on what's currently happening.</p>
3951cb0ef41Sopenharmony_ci<p>Objects are flattened following this format, so if you had
3961cb0ef41Sopenharmony_ci<code>{"scripts":{"install":"foo.js"}}</code> in your package.json, then you'd
3971cb0ef41Sopenharmony_cisee this in the script:</p>
3981cb0ef41Sopenharmony_ci<pre><code class="language-bash">process.env.npm_package_scripts_install === "foo.js"
3991cb0ef41Sopenharmony_ci</code></pre>
4001cb0ef41Sopenharmony_ci<h3 id="examples">Examples</h3>
4011cb0ef41Sopenharmony_ci<p>For example, if your package.json contains this:</p>
4021cb0ef41Sopenharmony_ci<pre><code class="language-json">{
4031cb0ef41Sopenharmony_ci  "scripts" : {
4041cb0ef41Sopenharmony_ci    "install" : "scripts/install.js",
4051cb0ef41Sopenharmony_ci    "postinstall" : "scripts/install.js"
4061cb0ef41Sopenharmony_ci  }
4071cb0ef41Sopenharmony_ci}
4081cb0ef41Sopenharmony_ci</code></pre>
4091cb0ef41Sopenharmony_ci<p>then <code>scripts/install.js</code> will be called for the install and post-install
4101cb0ef41Sopenharmony_cistages of the lifecycle.  Since <code>scripts/install.js</code> is running for two
4111cb0ef41Sopenharmony_cidifferent phases, it would be wise in this case to look at the
4121cb0ef41Sopenharmony_ci<code>npm_lifecycle_event</code> environment variable.</p>
4131cb0ef41Sopenharmony_ci<p>If you want to run a make command, you can do so.  This works just
4141cb0ef41Sopenharmony_cifine:</p>
4151cb0ef41Sopenharmony_ci<pre><code class="language-json">{
4161cb0ef41Sopenharmony_ci  "scripts" : {
4171cb0ef41Sopenharmony_ci    "preinstall" : "./configure",
4181cb0ef41Sopenharmony_ci    "install" : "make &amp;&amp; make install",
4191cb0ef41Sopenharmony_ci    "test" : "make test"
4201cb0ef41Sopenharmony_ci  }
4211cb0ef41Sopenharmony_ci}
4221cb0ef41Sopenharmony_ci</code></pre>
4231cb0ef41Sopenharmony_ci<h3 id="exiting">Exiting</h3>
4241cb0ef41Sopenharmony_ci<p>Scripts are run by passing the line as a script argument to <code>sh</code>.</p>
4251cb0ef41Sopenharmony_ci<p>If the script exits with a code other than 0, then this will abort the
4261cb0ef41Sopenharmony_ciprocess.</p>
4271cb0ef41Sopenharmony_ci<p>Note that these script files don't have to be Node.js or even
4281cb0ef41Sopenharmony_ciJavaScript programs. They just have to be some kind of executable
4291cb0ef41Sopenharmony_cifile.</p>
4301cb0ef41Sopenharmony_ci<h3 id="best-practices">Best Practices</h3>
4311cb0ef41Sopenharmony_ci<ul>
4321cb0ef41Sopenharmony_ci<li>Don't exit with a non-zero error code unless you <em>really</em> mean it.
4331cb0ef41Sopenharmony_ciIf the failure is minor or only will prevent some optional features, then
4341cb0ef41Sopenharmony_ciit's better to just print a warning and exit successfully.</li>
4351cb0ef41Sopenharmony_ci<li>Try not to use scripts to do what npm can do for you.  Read through
4361cb0ef41Sopenharmony_ci<a href="../configuring-npm/package-json.html"><code>package.json</code></a> to see all the things that you can specify and enable
4371cb0ef41Sopenharmony_ciby simply describing your package appropriately.  In general, this
4381cb0ef41Sopenharmony_ciwill lead to a more robust and consistent state.</li>
4391cb0ef41Sopenharmony_ci<li>Inspect the env to determine where to put things.  For instance, if
4401cb0ef41Sopenharmony_cithe <code>npm_config_binroot</code> environment variable is set to <code>/home/user/bin</code>, then
4411cb0ef41Sopenharmony_cidon't try to install executables into <code>/usr/local/bin</code>.  The user
4421cb0ef41Sopenharmony_ciprobably set it up that way for a reason.</li>
4431cb0ef41Sopenharmony_ci<li>Don't prefix your script commands with "sudo".  If root permissions
4441cb0ef41Sopenharmony_ciare required for some reason, then it'll fail with that error, and
4451cb0ef41Sopenharmony_cithe user will sudo the npm command in question.</li>
4461cb0ef41Sopenharmony_ci<li>Don't use <code>install</code>. Use a <code>.gyp</code> file for compilation, and <code>prepare</code>
4471cb0ef41Sopenharmony_cifor anything else. You should almost never have to explicitly set a
4481cb0ef41Sopenharmony_cipreinstall or install script. If you are doing this, please consider if
4491cb0ef41Sopenharmony_cithere is another option. The only valid use of <code>install</code> or <code>preinstall</code>
4501cb0ef41Sopenharmony_ciscripts is for compilation which must be done on the target architecture.</li>
4511cb0ef41Sopenharmony_ci<li>Scripts are run from the root of the package folder, regardless of what the
4521cb0ef41Sopenharmony_cicurrent working directory is when <code>npm</code> is invoked. If you want your
4531cb0ef41Sopenharmony_ciscript to use different behavior based on what subdirectory you're in, you
4541cb0ef41Sopenharmony_cican use the <code>INIT_CWD</code> environment variable, which holds the full path you
4551cb0ef41Sopenharmony_ciwere in when you ran <code>npm run</code>.</li>
4561cb0ef41Sopenharmony_ci</ul>
4571cb0ef41Sopenharmony_ci<h3 id="see-also">See Also</h3>
4581cb0ef41Sopenharmony_ci<ul>
4591cb0ef41Sopenharmony_ci<li><a href="../commands/npm-run-script.html">npm run-script</a></li>
4601cb0ef41Sopenharmony_ci<li><a href="../configuring-npm/package-json.html">package.json</a></li>
4611cb0ef41Sopenharmony_ci<li><a href="../using-npm/developers.html">npm developers</a></li>
4621cb0ef41Sopenharmony_ci<li><a href="../commands/npm-install.html">npm install</a></li>
4631cb0ef41Sopenharmony_ci</ul></div>
4641cb0ef41Sopenharmony_ci
4651cb0ef41Sopenharmony_ci<footer id="edit">
4661cb0ef41Sopenharmony_ci<a href="https://github.com/npm/cli/edit/latest/docs/content/using-npm/scripts.md">
4671cb0ef41Sopenharmony_ci<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
4681cb0ef41Sopenharmony_ci<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>
4691cb0ef41Sopenharmony_ci</svg>
4701cb0ef41Sopenharmony_ciEdit this page on GitHub
4711cb0ef41Sopenharmony_ci</a>
4721cb0ef41Sopenharmony_ci</footer>
4731cb0ef41Sopenharmony_ci</section>
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci
4761cb0ef41Sopenharmony_ci
4771cb0ef41Sopenharmony_ci</body></html>