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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read