How to use npm Scripts as a Build Tool ?
Last Updated :
24 Apr, 2024
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 variables cross-platform. These scripts are housed in the package.json file, which acts as a central configuration hub for npm-managed projects.
Why Choose NPM Scripts for Builds?
In summary, npm scripts provide a lightweight yet powerful solution for automating your project builds. By defining simple commands in your package.json file, you can streamline your development workflow and spend more time coding and less time managing build tools.
- Simplicity: NPM scripts offer a straightforward approach to building your projects. No need to juggle multiple build tools or configurations.
- Integration: Since npm scripts are part of the npm ecosystem, they seamlessly integrate with your existing workflow. No extra installations are NPM required!
- Customization: With npm scripts, you have the flexibility to tailor your build process to suit your project's unique requirements.
Steps to create NPM scripts:
Let's walk through the process of setting up npm scripts for a basic HTML, CSS, and JavaScript project:
Initialize a new npm project:
Begin by initializing a new npm project within your project directory:
npm init -y
This command creates a package.json file with default settings.
Define your build scripts:
For a simple project involving HTML, CSS, and JavaScript(code given below), you can set up scripts to copy files from the src directory to the dist directory.
Now, let's define the npm script in the package.json file. Open package.json and add the following:
"scripts": {
"build": "mkdir -p dist && cp src/index.html dist/ && cp src/style.css dist/ && cp src/script.js dist/"
}
Here, the build script creates a dist directory (if it doesn't exist) and copies the HTML, CSS, and JavaScript files from the src directory to the dist directory.
Run the Project:
To run an HTML file with live-server, you can use the live-server package. First, you need to install it globally (if you haven't already) using npm:
npm install -g live-server
After installing live-server, you can use it to serve your HTML file by navigating to the directory containing your HTML file in your terminal and running:
live-server
This command will start a local server and open your default web browser to display your HTML file. It also provides features like live reloading, so any changes you make to your HTML file
Note: You can also run this project By Right clicking on index.html file by clicking on "Open with Live Server"

Run your build script:
Execute your build script using the npm run command followed by the script name:
npm run build
This command triggers the defined build tasks, generating a distribution-ready version of your project in the dist directory.
Package.json file:
package.jsonFolder Structure:

Example: To demonstrate creating an simple JavaScript project and using the npm script as build tool in it.
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>My Project</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>GeeksforGeeks!</h1>
<script src="script.js"></script>
</body>
</html>
CSS
/* style.css */
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
h1 {
color: green;
}
JavaScript
//src/script.js
console.log("Script loaded!");
Output:
output
Similar Reads
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 yo
5 min read
How to NPM Run Start At The Background? Running npm start or npm run start in the background is one of those things that may not realized until you are knee-deep in a project. Whether you are building a web application, working with microservice, or just trying to keep your terminal free from having a server run without locking your termi
3 min read
How To Fix ânpm err! missing script: startâ? While working on a Node.js project sometimes we encounter an error "npm ERR! missing script: start". This error message appears when we try to start your application but something is missing or misconfigured in our project. This is a common issue and can be fixed very easily with very few steps. In
3 min read
How to Set Up a Private NPM Registry In this article we will learn how to install and configure a private NPM registry, and control authentication and access controls to users, also we will learn how to publish packages to npm and download and use them in our project. In this article, we will use Verdaccio, a lightweight and easy-to-us
6 min read
How to build and publish an NPM package for React using Typescript? In development, creating and distributing reusable components is essential for building scalable and maintainable applications. With the popularity of TypeScript and React, you can easily package and share your components as NPM packages. This tutorial will teach us how to create and release our NPM
4 min read
Introduction to NPM scripts 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 t
2 min read
How to use External Modules and NPM in a project ? Need for External Modules: For a large JavaScript application, it becomes difficult and messy to write the whole code in just one JavaScript file. This is where CommonJS comes into the picture and this CommonJS format defines a module format that can be used up for breaking your JS application into
3 min read
How to Change npm start Script of Node.js ? In Node.js, npm (Node Package Manager) provides a convenient way to manage project scripts through the scripts field in the package.json file. By default, the npm start command is used to start your Node.js application. However, you might need to customize this command to suit specific requirements,
3 min read
How To Build A Node.js API For Ethereum Ethereum is a popular blockchain platform that allows developers to build decentralized applications (dApps). It uses a smart contract system that enables the execution of self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code.The
8 min read
Build an Online Code Compiler using React.js and Node.js In this article, we will learn how to build an online code compiler using React.js as frontend and Express.js as backend. Users will be able to write C, C++, Python, and Java code with proper syntax highlighting as well as compile and execute it online. The main objective of building an online compi
7 min read