The Wayback Machine - https://web.archive.org/web/20240708094814/https://www.geeksforgeeks.org/introduction-to-npm-scripts/
Open In App

Introduction to NPM scripts

Last Updated : 05 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

NPM is a Node Package Manager. It is the world’s largest Software Registry. This registry contains over 800,000 code packages. Many Open-source developers use npm to share software. Many organizations also use npm to manage private development.

“npm scripts” are the entries in the scripts field of the package.json file. The scripts field holds an object where you can specify various commands and scripts that you want to expose. These can be executed using the following command-

npm run <script-name>

NPM scripts are used to automate tasks like minifying CSS, uglifying JavaScript, building project. NPM scripts are versatile and simple and by learning fewer tools, we can automate tasks in our web project.

For example, this is our package.json file.

{
  "name": "example",
  "scripts": {
    "test": "echo 'hello world'"
  }
}

and it can be run by using the following command –

npm run test

This is very useful when we have repetitive tasks and we have to automate them.

NPM scripts within another NPM scripts:

{
  "name": "example",
  "scripts": {
    "start": "npm run lite",
    "lite": "lite-server" 
  }
}

So by using npm start, it will run another command npm run lite, lite is lite-server. So the command that run is npm run lite-server, this will cause node server to run.

Web development and deployment requires a lot of repetitive tasks, So there was a need for tools that automate these tasks.

So here are some tasks which can be automated.

CSS Tasks:

  • Compiling Sass or Less into CSS.
  • Running Autoprefixer to add vendor prefixes that are needed.
  • Minification: removing unnecessary characters (white spaces, newlines, comments) from source code without compromising functionality.
  • Concatenation

JavaScript Tasks:

  • JSHint: Checking JavaScript code for errors and potential problems (static code analysis).
  • Concatenation
  • Uglification: minification + mangling (reduce local variables to single letters).
  • Rechecking for errors.

Building Project:

  • Projects can be build that consists of all files and folders requires and several dependencies.
  • some of the packages which is used are rimraf, copyfiles, usemin, cssmin, htmlmin, uglifyjs.

Image Compression & BrowserSync:

  • Images are compressed using imagemin.
  • Automatically browser is rendered when changes are done using onchange, watch.


Similar Reads

How to use npm Scripts as a Build Tool ?
In the world of JavaScript development, npm (Node Package Manager) isn't just for installing packages. It also allows you to define and run scripts to automate various tasks within your project. One can make use of npm packages within your scripts to extend functionality. For instance, you can employ packages like cross-env to set environment varia
3 min read
How to Run, Configure, and Troubleshoot npm Scripts?
npm (Node Package Manager) is not only used for managing dependencies in Node.js projects but also provides a powerful script-running functionality. npm scripts allow you to automate various tasks such as running tests, building your project, deploying applications, and more. This guide will walk you through the process of running, configuring, and
5 min read
How to Run Multiple NPM Scripts in Parallel?
Running multiple npm scripts in parallel can be particularly useful when you need to perform several tasks simultaneously, such as starting a server, running a build process, and monitoring file changes. This can save time and streamline your development workflow. Here’s a comprehensive guide on how to run multiple npm scripts in parallel. Table of
3 min read
Difference between npm i and npm ci in Node.js
The following difference covers how npm i and npm ci command are different from each other and their functioning. The npm which is called a node package manager which is used for managing modules needed for our application. npm i: The npm i (or npm install) is used to install all dependencies or devDependencies from a package.json file. Syntax: npm
2 min read
Difference between npm install and npm update in Node.js
NPM is like a powerhouse for Node.js that contains all the necessary modules for the smooth running of the node.js application. It gets installed on our machine when we install Node.js on our Windows, Linux or MAC OS. How to install Node on the machine? Refer to this article. NPM has 580096 registered packages. The average rate of growth of this nu
5 min read
How to solve npm error npm ERR! code ELIFECYCLE ?
In order to solve the "npm ERR! code ELIFECYCLE " error which is a very common type of error that occurs during npm operation on our command prompt or terminal such as installing npm or an npm package, follow the steps given below : Terminal output of the error : Follow below steps to fix the error : Step 1 : In our first step we will try cleaning
2 min read
How to Get a List of Globally Installed NPM Packages in npm ?
Learning how to retrieve a list of globally installed NPM packages is essential for developers managing their Node.js environment. Utilizing npm, the Node Package Manager, enables users to efficiently manage packages across projects. This article will guide you through getting access to global NPM packages, which will further help you to enhance yo
2 min read
Axios npm - How to Install Axios npm in Terminal?
Axios is a popular JavaScript library for making HTTP requests in web applications. It simplifies the process of sending asynchronous requests and handling responses. It is commonly used with Node.js and in browser-based projects and can be installed via NPM (Node Package Manager). In this article, we will explore the step-by-step process of instal
2 min read
npm cache clean - How to Clear the Cache in NPM ?
Clearing the cache in NPM (Node Package Manager) is very important. It helps to resolve various issues and ensures smooth functioning of the Node.js projects. The NPM cache stores downloaded packages and their metadata, which can sometimes become corrupted or outdated, leading to installation problems, dependency conflicts, or other issues. Table o
5 min read
Remove NPM - npm uninstall
To remove npm (Node Package Manager) from your macOS system, you can't use npm uninstall since npm itself doesn't support uninstalling itself. Instead, you need to remove it manually along with Node.js. Table of Content What is NPM?What is NPM remove?Installing a package using npmUninstalling a Package using npmRemove npmRemove a Dev DependencyWhat
2 min read
three90RightbarBannerImg