<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: S.P.</title>
    <description>The latest articles on DEV Community by S.P. (@asap).</description>
    <link>https://dev.to/asap</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F911089%2F1976173b-6482-4048-a8c9-89c40a572fd5.png</url>
      <title>DEV Community: S.P.</title>
      <link>https://dev.to/asap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asap"/>
    <language>en</language>
    <item>
      <title>NPM scripts memo</title>
      <dc:creator>S.P.</dc:creator>
      <pubDate>Sat, 24 Dec 2022 23:32:22 +0000</pubDate>
      <link>https://dev.to/asap/npm-scripts-summary-5012</link>
      <guid>https://dev.to/asap/npm-scripts-summary-5012</guid>
      <description>&lt;p&gt;There is an extremely brief but too informative summary about such convenient feature of npm as &lt;em&gt;scripts&lt;/em&gt;. So few words, only grouped and classified facts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Disclamer
&lt;/h3&gt;

&lt;p&gt;Information is relevant for npm v9.2.0&lt;/p&gt;

&lt;h3&gt;
  
  
  Main purposes of this post
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;to introduce classification of scripts&lt;/li&gt;
&lt;li&gt;to summarize important things about &lt;em&gt;npm scripts&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;to act as a quick reminder about &lt;em&gt;npm scripts&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;The readers should already be familiar with &lt;em&gt;npm scripts&lt;/em&gt; feature at least at elementary level. It can be learned from scratch using some other sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/paulasantamaria/mastering-npm-scripts-2chd"&gt;DEV.TO post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/nodejs-utilizing-npm-scripts-during-development"&gt;DigitalOcean post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.npmjs.com/cli/v9/using-npm/scripts"&gt;NPM docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Classification
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
Natively supported scripts

&lt;ul&gt;
&lt;li&gt;Lifecycle&lt;/li&gt;
&lt;li&gt;Action-specific&lt;/li&gt;
&lt;li&gt;Command-specific&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Arbitrary scripts&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1 Natively supported
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Purpose
&lt;/h4&gt;

&lt;p&gt;To include developer-defined actions into npm command's flow (e.g., &lt;em&gt;npm pack, npm install&lt;/em&gt;)&lt;/p&gt;

&lt;h4&gt;
  
  
  Where defined
&lt;/h4&gt;

&lt;p&gt;package.json, &lt;code&gt;"scripts"&lt;/code&gt; object&lt;/p&gt;

&lt;h4&gt;
  
  
  When executed
&lt;/h4&gt;

&lt;p&gt;As a part of certain &lt;em&gt;npm&lt;/em&gt; command - before or after the main action that performed by this command&lt;/p&gt;

&lt;h4&gt;
  
  
  How to run
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;npm &amp;lt;command-name&amp;gt;&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;Native scripts can be classified into &lt;strong&gt;Lifecycle&lt;/strong&gt;, &lt;strong&gt;Action-specific&lt;/strong&gt;, &lt;strong&gt;Command-specific&lt;/strong&gt; scripts.&lt;/p&gt;




&lt;h3&gt;
  
  
  1-1 Lifecycle scripts
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Description
&lt;/h4&gt;

&lt;p&gt;Wide specific scripts. Run in the flow of several &lt;em&gt;npm&lt;/em&gt; commands.&lt;/p&gt;

&lt;h4&gt;
  
  
  Scripts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;preprepare/prepare/postprepare&lt;/li&gt;
&lt;li&gt;dependencies &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;prepare&lt;/em&gt; script is run in the flow of...&lt;br&gt;

  ...next commands:
  &lt;ul&gt;
&lt;li&gt;npm cache add&lt;/li&gt;
&lt;li&gt;npm ci&lt;/li&gt;
&lt;li&gt;npm diff&lt;/li&gt;
&lt;li&gt;npm install&lt;/li&gt;
&lt;li&gt;npm pack&lt;/li&gt;
&lt;li&gt;npm publish&lt;/li&gt;
&lt;li&gt;npm rebuild
&lt;/li&gt;
&lt;/ul&gt;




&lt;/p&gt;
&lt;p&gt;See more &lt;a href="https://docs.npmjs.com/cli/v9/using-npm/scripts#life-cycle-scripts"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  1-2 Action-specific scripts
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Description
&lt;/h4&gt;

&lt;p&gt;Run in the flow of one &lt;em&gt;npm&lt;/em&gt; command along with command's main action. Some scripts run in more than one commands those perform related actions.&lt;/p&gt;

&lt;h4&gt;
  
  
  Scripts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;prepublish - in flow of &lt;code&gt;npm ci&lt;/code&gt;, &lt;code&gt;npm install&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;prepublishOnly - in flow of &lt;code&gt;npm publish&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;prepack/postpack - in flow of &lt;code&gt;npm pack&lt;/code&gt;, &lt;code&gt;npm publish&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;preinstall/install/postinstall - in flow of &lt;code&gt;npm ci&lt;/code&gt;, &lt;code&gt;npm install&lt;/code&gt;, &lt;code&gt;npm rebuild&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;preversion/version/postversion - in flow of &lt;code&gt;npm version&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;npm version&lt;/code&gt; commands run the flow that includes its main action and predefined scripts. Execution order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;preversion&lt;/em&gt; script&lt;/li&gt;
&lt;li&gt;npm version &lt;strong&gt;command's action&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;version&lt;/em&gt; script&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;postversion&lt;/em&gt; script&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See more &lt;a href="https://docs.npmjs.com/cli/v9/using-npm/scripts#life-cycle-operation-order"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  1-3 Command-specific scripts
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Description
&lt;/h4&gt;

&lt;p&gt;Run in response of exactly one built-in &lt;em&gt;npm&lt;/em&gt; command. These commands don't have their own actions and only run predefined scripts&lt;/p&gt;

&lt;h4&gt;
  
  
  Scripts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;prestart/start/poststart - in response of &lt;code&gt;npm start&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;prerestart/restart/postrestart - in response of &lt;code&gt;npm restart&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;prestop/stop/poststop - in response of &lt;code&gt;npm stop&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;pretest/test/posttest - in response of &lt;code&gt;npm test&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;npm start&lt;/code&gt; performs the next operations in order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;prestart&lt;/em&gt; script&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;start&lt;/em&gt; script &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;poststart&lt;/em&gt; script&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This command doesn't have its own action other than scripts running.&lt;/p&gt;




&lt;h2&gt;
  
  
  2 Arbitrary
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Purpose
&lt;/h4&gt;

&lt;p&gt;To perform developer-defined tasks using npm ecosystem&lt;/p&gt;

&lt;h4&gt;
  
  
  Motivation
&lt;/h4&gt;

&lt;p&gt;There may be other ways to perform developer-defined tasks, but it's handy to use &lt;em&gt;npm&lt;/em&gt; ecosystem for them&lt;/p&gt;

&lt;h4&gt;
  
  
  How to define:
&lt;/h4&gt;

&lt;p&gt;package.json, &lt;code&gt;"scripts"&lt;/code&gt; object&lt;/p&gt;

&lt;h4&gt;
  
  
  When executed
&lt;/h4&gt;

&lt;p&gt;In response of &lt;em&gt;npm run &lt;/em&gt; command that runs exactly certain script named &lt;em&gt;script-name&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  How to run
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;npm run[-script] &amp;lt;script-name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Important
&lt;/h4&gt;

&lt;p&gt;Arbitrary scripts can have &lt;strong&gt;pre/post&lt;/strong&gt; versions&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;Given we have &lt;em&gt;precompress&lt;/em&gt;, &lt;em&gt;compress&lt;/em&gt; and &lt;em&gt;postcompress&lt;/em&gt; scripts in &lt;em&gt;package.json&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm run[-script] compress&lt;/code&gt; performs the next:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;precompress script&lt;/li&gt;
&lt;li&gt;compress script &lt;/li&gt;
&lt;li&gt;postcompress script&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So arbitrary scripts are similar to Command-specific native scripts. But they don't have a specific built-in &lt;em&gt;npm&lt;/em&gt; command for running them.&lt;/p&gt;




&lt;p&gt;This was a brief summary about &lt;em&gt;npm scripts&lt;/em&gt;. I'm glad to receive a feedback!&lt;/p&gt;

</description>
      <category>npm</category>
      <category>node</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
