NPM scripts memo
Tags: npm, node, javascript, programming
Published: 2022-12-24 23:32:22

There is an extremely brief but too informative summary about such convenient feature of npm as _scripts_. So few words, only grouped and classified facts.

### Disclamer
Information is relevant for npm v9.2.0

### Main purposes of this post
- to introduce classification of scripts
- to summarize important things about _npm scripts_
- to act as a quick reminder about _npm scripts_

### Prerequisites
The readers should already be familiar with _npm scripts_ feature at least at elementary level. It can be learned from scratch using some other sources:
- [DEV.TO post](https://dev.to/paulasantamaria/mastering-npm-scripts-2chd)
- [DigitalOcean post](https://www.digitalocean.com/community/tutorials/nodejs-utilizing-npm-scripts-during-development)
- [NPM docs](https://docs.npmjs.com/cli/v9/using-npm/scripts)

---

### Classification

1. [Natively supported](#1-natively-supported) scripts
  * [Lifecycle](#11-lifecycle-scripts)
  * [Action-specific](#12-actionspecific-scripts)
  * [Command-specific](#13-commandspecific-scripts)
2. [Arbitrary](#2-arbitrary) scripts

---

## 1 Natively supported

#### Purpose
To include developer-defined actions into npm command's flow (e.g., _npm pack, npm install_)
#### Where defined
package.json, `"scripts"` object
#### When executed
As a part of certain _npm_ command - before or after the main action that performed by this command
#### How to run
`npm <command-name>`

---

Native scripts can be classified into **Lifecycle**, **Action-specific**, **Command-specific** scripts.

---

### 1-1 Lifecycle scripts
#### Description
Wide specific scripts. Run in the flow of several _npm_ commands.
#### Scripts
  * preprepare/prepare/postprepare
  * dependencies 

#### Example
_prepare_ script is run in the flow of...
{% details ...next commands: %}
- npm cache add
- npm ci
- npm diff
- npm install
- npm pack
- npm publish
- npm rebuild
{% enddetails %}

See more [here](https://docs.npmjs.com/cli/v9/using-npm/scripts#life-cycle-scripts).

---

### 1-2 Action-specific scripts
#### Description
Run in the flow of one _npm_ command along with command's main action. Some scripts run in more than one commands those perform related actions.

#### Scripts
 * prepublish - in flow of `npm ci`, `npm install`
 * prepublishOnly - in flow of `npm publish`
 * prepack/postpack - in flow of `npm pack`, `npm publish`
 * preinstall/install/postinstall - in flow of `npm ci`, `npm install`, `npm rebuild`
 * preversion/version/postversion - in flow of `npm version`

#### Example

`npm version` commands run the flow that includes its main action and predefined scripts. Execution order:
- _preversion_ script
- npm version **command's action**
- _version_ script
- _postversion_ script

See more [here](https://docs.npmjs.com/cli/v9/using-npm/scripts#life-cycle-operation-order).

---

### 1-3 Command-specific scripts
#### Description
Run in response of exactly one built-in _npm_ command. These commands don't have their own actions and only run predefined scripts

#### Scripts
  * prestart/start/poststart - in response of `npm start`
  * prerestart/restart/postrestart - in response of `npm restart`
  * prestop/stop/poststop - in response of `npm stop`
  * pretest/test/posttest - in response of `npm test`

#### Example
`npm start` performs the next operations in order:
- _prestart_ script
- _start_ script 
- _poststart_ script

This command doesn't have its own action other than scripts running.

---

## 2 Arbitrary

#### Purpose
To perform developer-defined tasks using npm ecosystem
#### Motivation
There may be other ways to perform developer-defined tasks, but it's handy to use _npm_ ecosystem for them

#### How to define:
package.json, `"scripts"` object

#### When executed
In response of _npm run <script-name>_ command that runs exactly certain script named _script-name_

#### How to run
`npm run[-script] <script-name>`

#### Important
Arbitrary scripts can have **pre/post** versions

#### Example
Given we have _precompress_, _compress_ and _postcompress_ scripts in _package.json_.

`npm run[-script] compress` performs the next:
- precompress script
- compress script 
- postcompress script


So arbitrary scripts are similar to [Command-specific](#13-command-specific-scripts) native scripts. But they don't have a specific built-in _npm_ command for running them.



---

This was a brief summary about _npm scripts_. I'm glad to receive a feedback!