This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Denvery
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:
Classification
-
Natively supported scripts
- Lifecycle
- Action-specific
- Command-specific
- 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 more deeply. See that below with examples.
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...
...next commands:
- npm cache add
- npm ci
- npm diff
- npm install
- npm pack
- npm publish
- npm rebuild
Action specific scripts
Description
Run in the flow of exactly one npm command or in several closely related commands.
Scripts
- prepublish/prepublishOnly
- prepack/postpack
- preinstall/install/postinstall
- preversion/version/postversion`
Example
npm version
commands run the flow that includes its main action + predefined scripts:
- preversion script
- npm version command's action
- version script
- postversion script
Command specific scripts
Description
Run in response of specific npm commands. These commands don't have their own actions other than running predefined scripts.
Scripts
- prestart/start/poststart
- prerestart/restart/postrestart
- prestop/stop/poststop
- pretest/test/posttest
Example
npm start
performs the next:
- 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 are other ways to perform developer-defined tasks, but it's very convenient to use npm ecosystem for them.
How to define:
package.json, "scripts"
object
When executed
In response of npm command that runs exactly certain script
How to run
npm run[-script] <script-name>
Important
Arbitrary scripts have pre/post versions.
Example
npm run[-script] compress
performs the next:
- precompress script
- compress script
- postcompress script
This was a brief summary about npm scripts. I'm glad to receive a feedback!
This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Denvery
Denvery | Sciencx (2022-12-24T23:32:22+00:00) NPM Scripts summary. Retrieved from https://www.scien.cx/2022/12/24/npm-scripts-summary/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.