What are NPM scripts?
© https://nodejs.org/en/

What are NPM scripts?

Bundle shell commands and automate repetitive tasks.

ByMario Kandut

honey pot logo

Europe’s developer-focused job platform

Let companies apply to you

Developer-focused, salary and tech stack upfront.

Just one profile, no job applications!

This article is based on Node v16.15.1 and NPM 8.11.0.

NPM scripts are a way to bundle shell commands. Typically, they are a string of commands, which would be entered the terminal, or they are part of a pipeline in a CI/CD.

What exactly are NPM scripts?

💰 The Pragmatic Programmer: journey to mastery. 💰 One of the best books in software development, sold over 200,000 times.

NPM scripts are terminal commands that run in a terminal. That's all there is to it. 🎉

Scripts are stored in the project's package.json file and everyone who has access to the project, also has access to these scripts. They help automate repetitive tasks, like generating types for an API, and can be shared within the project, and you have to learn fewer tools.

Common use cases for npm scripts are:

  • building your Node project (think of multiple environments like develop and production)
  • starting a development server
  • compiling SCSS to CSS
  • linting
  • minifying
  • testing
  • basically anything you repetitive type in the shell during the project.

Scripts are executed using the npm run NAME-OF-YOUR-SCRIPT command. There are also some predefined aliases that convert to npm run, like npm test or npm start, you can use them interchangeably.

There are also pre- and post-hooks, which are scripts that run before a script lifecycle event. For example, running npm start would first trigger prestart, then start and finally poststart. You can also use only one of these lifecycle hooks, like postinstall, which would run automatically after every npm install or npm install <package> command.

To create pre or post scripts for any scripts defined in the scripts section of the package.json, simply create another script with a matching name and add pre or post to the beginning of them. For example:

  "scripts": {
    "precompress": "{{ executes BEFORE the `compress` script }}",
    "compress": "{{ run command to compress files }}",
    "postcompress": "{{ executes AFTER `compress` script }}"

In the above example running the command npm run compress would execute these scripts as described. Read more about Pre & Post Scripts in the NPM docs.

Simple NPM script

It's very common to have commands that you run regularly as an npm script, like linting, building production apps, starting your application, etc.

A simple example for a npm script would be:

"scripts": {
	"start": "node index.js",

Running npm start with the above script would start a node server. Commands like this come very handy, when you have to set additional parameters and/or flags, when starting an application. Let's say you want to set the port node index.js --port=8000, just replace the command and running npm start would do that.

Compose NPM script

You can also compose npm scripts, so scripts can call other scripts.

"scripts": {
	"start": "node index.js",
	"dev": "NODE_ENV=development npm run lint && npm start",
	"lint": "eslint ./*.js",
  • Running npm start would start a node server.
  • Running npm run lint would lint all the *.js files.
  • Running npm run dev would first set the node environment variable to development, then run npm run lint and then start the application with npm start.

Scripts have the ability to access commands provided by installed packages by name, no complete path required. The npm run command will first try, and resolve to a locally-installed package (node_modules), if this fails, it falls back to a globally-installed package.


  • NPM scripts let you bundle aliases for commonly executed commands.
  • Script definitions are stored in the scripts key in the package.json file.
  • NPM scripts are executed via npm run <YOUR-SCRIPT-NAME>.

Thanks for reading and if you have any questions, use the comment function or send me a message @mariokandut.

If you want to know more about Node, have a look at these Node Tutorials.

References (and Big thanks):

NPM docs, HeyNode

More node articles:

Getting started with Webpack

How to list/debug npm packages?

How to specify a Node.js version

How to create a web server in Node.js

How to dynamically load ESM in CJS

How to convert a CJS module to an ESM

How to create a CJS module

How to stream to an HTTP response

How to handle binary data in Node.js?

How to use streams to ETL data?

How to connect streams with pipeline?

How to handle stream errors?

How to connect streams with pipe?

What Is a Node.js Stream?

Handling Errors in Node (asynchronous)

Handling Errors in Node.js (synchronous)

Introduction to errors in Node.js

Callback to promise-based functions

ETL: Load Data to Destination with Node.js

ETL: Transform Data with Node.js

ETL: Extract Data with Node.js

Event Emitters in Node.js

How to set up SSL locally with Node.js?

How to use async/await in Node.js

What is an API proxy?

How to make an API request in Node.js?

How does the Event Loop work in Node.js

How to wait for multiple Promises?

How to organize Node.js code

Understanding Promises in Node.js

How does the Node.js module system work?

Set up and test a .env file in Node

How to Use Environment Variables in Node

How to clean up node modules?

Restart a Node.js app automatically

How to update a Node dependency - NPM?

What are NPM scripts?

How to uninstall npm packages?

How to install npm packages?

How to create a package.json file?

What Is the Node.js ETL Pipeline?

What is data brokering in Node.js?

How to read and write JSON Files with Node.js?

What is package-lock.json?

How to install Node.js locally with nvm?

How to update Node.js?

How to check unused npm packages?

What is the Node.js fs module?

What is Semantic versioning?

The Basics of Package.json explained

How to patch an NPM dependency

What is NPM audit?

Beginner`s guide to NPM

Getting started with Node.js

Scroll to top ↑