Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconIntroduction to Package JSON Scripts in Node JS

Introduction to Package JSON Scripts in Node JS

Last updated:
11th Apr, 2023
Views
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
Introduction to Package JSON Scripts in Node JS

Package JSON scripts consist of predefined commands that can be executed via the command line using the Node Package Manager (npm). These npm scripts are defined in the package.json file, which is the configuration file for node package json projects. The scripts provide a convenient way of executing common tasks such as starting a server, building the project, running tests, and deploying the application.

By using package.json scripts, developers can avoid remembering long and complicated command-line arguments for executing tasks. Instead, the scripts can be executed using simple, descriptive commands such as npm start or npm test.

What is Package JSON?

Package JSON is a plain text JSON (JavaScript Object Notation) file used to define the metadata of a Node.js project. The package json in node js acts as the central configuration file for Node.js projects and contains essential information like the project’s name, version, description, dependencies, devDependencies, and much more. 

The package.json scripts are created when a developer initialises a Node.js project using the “npm init” command. These scripts are utilised by Node Package Manager (npm) to install dependencies, manage versions, and execute various scripts.

Ads of upGrad blog

Check out our free technology courses to get an edge over the competition.

Benefits of using Package JSON Scripts

There are many benefits to using the package json in node js projects:

  • Simplified command execution: Package JSON scripts provide a simple and consistent way to execute commonly used commands to start the application, run tests, and deploy. Developers don’t need to remember a long list of commands to execute tasks. 
  • Cross-platform compatibility: Package JSON scripts are platform-independent, making them compatible with any platform that supports Node.js. This makes it easier for developers to transition between development environments. 
  • Automated workflows: Using scripts in the package.json file, developers can automate repetitive tasks such as running unit tests or minifying files. This reduces the chances of human error and gives the developers more time to focus on more important tasks.
  • Ease of sharing code: Package JSON scripts can be shared along with the codebase; this makes it easier for the developers to set up their development environment and run tasks quicker.
  • Consistent environment: All the developers use the same version of scripts. Scripts can be used to manage and install dependencies, ensuring that all developers are using the same versions of dependencies, thus reducing the likelihood of compatibility issues.

Acquiring proper software knowledge enables developers to adopt these benefits, effectively reducing their workload and optimising program and task management, resulting in significant time savings. If you want to enhance your skills in this domain, upGrad, India’s pioneering urban multidisciplinary education platform, offers a Full Stack Software Development Bootcamp. This program empowers students to become top-notch software developers.

Commonly used Package JSON Scripts

Some commonly used Package JSON scripts are:

  • “start”: This script is used to start the server or application. For example, “start”: “node index.js”. 
  • “test”: This script is used to run tests on the project. For example, “test”: “mocha tests/”. 
  • “build”: This script is used to build the project. For example, “build”: “webpack –mode production”. 
  • “dev”: This script is used to start a development server with live-reloading. For example, “dev”: “nodemon index.js”.
  • “lint”: This script is used to run code style checks using a linter. For example, “lint”: “eslint src/”. 
  • “prestart” and “poststart”: These scripts are run before and after the “start” script, respectively. 
  • “pretest” and “posttest”: These scripts are run before and after the “test” script, respectively. 
  • “prebuild” and “postbuild”: These scripts are run before and after the “build” script, respectively. 

These are a few examples of the commonly used package.json scripts used by developers. They can create their own custom commands for convenience. To execute these scripts, use the command npm run <scriptname> in the terminal.

Explore Our Software Development Free Courses

Running Package JSON Scripts with NPM

Package JSON scripts can run using the NPM (Node Package Manager). Here are the steps to run a script using NPM: 

  1. Open the terminal or command prompt. 
  2. Navigate to the project directory using the cd command. 
  3. Type npm run <scriptname> to execute the script. 

For example, if you have a “start” script defined in your package.json file, you can run it using the command npm run start. This will execute the command specified in the “start” script.

If you have a custom script in the package.json file named “my-script”, you can execute it using the command npm run my-script. 

When using NPM to run a script, the command will execute the script and display the output in the terminal. If there are any error or warning messages, it will also be shown in the terminal.

In addition to running scripts, NPM serves as a versatile tool with various functionalities. It enables developers to manage dev dependencies, install and publish packages to the NPM registry. Widely embraced within the Node.js community, NPM is an essential tool for effectively managing Node.js projects.

Check Out upGrad’s Software Development Courses to upskill yourself.

Creating Custom Package JSON Scripts

Creating custom Package JSON scripts in a Node.js project is very easy. Here are the steps to create it

  1. Open the package.json file in your project directory. 
  2. Add a new property to the scripts object with a unique name for the script. 
  3. Set the value of the new property to the command or series of commands you want to execute. 

For example, if you want to create a custom script to copy a file from one directory to another. Here’s how you would define a “copy-file” script:

 “scripts”: { “copy-file”: “cp src/file.txt dist/” } 

The above script defines a new “copy-file” script that uses the cp command to copy the file.txt file from the src directory to the dist directory. 

You would run the command npm run copy-file in the terminal to execute the script.

Custom scripts can be used to automate repetitive tasks, run tests, or execute system commands. They provide an easy way to streamline the development process and save time.

Passing Arguments to Package JSON Scripts

Developers can pass arguments to Package JSON scripts. They define a custom script in the package.json file and use the npm_config_my_arg string as a placeholder for the argument.

In the terminal, they can pass the argument by prefixing it with — and running the script using the npm run <scriptname> command. Additionally, environment variables can also be used to pass arguments by reading them within a script using the process.env object. 

For example, npm remove packages; developers can pass the package name as an argument and use the npm uninstall command within a custom script.

Explore our Popular Software Engineering Courses

Running Multiple Scripts in Parallel

“npm-run-all” is a package that allows developers to run multiple scripts parallelly. To run multiple scripts in parallel using “npm-run-all”, developers need to specify the scripts in the package.json file and use the run-p command to start them. In the terminal, developers can run the specified script. Running scripts in parallel can help speed up developments and make it easier to work on multiple tasks simultaneously. However, the scripts must be handled with care and ensure that they do not interfere with each other and are designed to run in parallel.

Mastering the art of running multiple scripts in parallel requires skill, and upGrad can help you acquire that skill. upGrad offers a course of Master of Science in Computer Science from LJMU that provides in-depth knowledge of computer science principles and practices. 

Best Practices for Writing Package JSON Scripts

Ads of upGrad blog

Some best practices for writing Package JSON Scripts are:

  1. Use descriptive script names. 
  2. Use lowercase and hyphen-separated words in script names. 
  3. Use shebang (#!/usr/bin/env) at the top of shell scripts. 
  4. Define scripts with cross-platform compatibility in mind. 
  5. Use third-party scripts with caution. 
  6. Include command-line arguments as required or appropriate in script files. 
  7. Update the script documentation as necessary to reflect changes to the scripts.

Conclusion

Package JSON scripts offer an effective way to manage Node.js project workflows using the npm package json manager. By defining scripts in the Package JSON file, developers can simplify command inputs, automate tasks, and ensure consistency across development environments. Moreover, custom scripts can be defined with placeholders to pass arguments, and npm-run-all can be used to run multiple scripts in parallel. The Package JSON file is a critical component of Node.js projects, and it is essential to keep it up-to-date to manage dependencies and execute scripts.

Among the wide range of resources to help you upskill, upGrad’s Executive PG Programme in Full Stack Development from IIITB is a leading option. This 13 months course specialises in software and, upon its completion, offers a degree from the renowned International Institute of Information Technology Bangalore (IIITB)!

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.

Frequently Asked Questions (FAQs)

1What is Package JSON?

Package JSON is a configuration file for Node.js projects that defines metadata about the project, such as name, version, dependencies, scripts, and more.

2How do I run Package JSON Scripts?

You can run the Package JSON scripts using the npm run command in the terminal.

3How to update Package JSON dependencies?

In order to update all the Package JSON dependencies, implement the command ‘npm update’. This will lead to update all dependencies as well as devDependenices segments in your Package JSON file.

4What is the use of package json?

The package. json file contains descriptive and functional metadata about a project, such as a name, version, and dependencies. The file provides the npm package manager with various information to help identify the project and handle dependencies.

Explore Free Courses

Suggested Blogs

Best Jobs in IT without coding
134263
If you are someone who dreams of getting into the IT industry but doesn’t have a passion for learning programming, then it’s OKAY! Let me
Read More

by Sriram

12 Apr 2024

Scrum Master Salary in India: For Freshers &#038; Experienced [2023]
900304
Wondering what is the range of Scrum Master salary in India? Have you ever watched a game of rugby? Whether your answer is a yes or a no, you might h
Read More

by Rohan Vats

05 Mar 2024

SDE Developer Salary in India: For Freshers &#038; Experienced [2024]
905061
A Software Development Engineer (SDE) is responsible for creating cross-platform applications and software systems, applying the principles of compute
Read More

by Rohan Vats

05 Mar 2024

System Calls in OS: Different types explained
5021
Ever wondered how your computer knows to save a file or display a webpage when you click a button? All thanks to system calls – the secret messengers
Read More

by Prateek Singh

29 Feb 2024

Marquee Tag &#038; Attributes in HTML: Features, Uses, Examples
5134
In my journey as a web developer, one HTML element that has consistently sparked both curiosity and creativity is the venerable Marquee tag. As I delv
Read More

by venkatesh Rajanala

29 Feb 2024

What is Coding? Uses of Coding for Software Engineer in 2024
5053
Introduction  The word “coding” has moved beyond its technical definition in today’s digital age and is now considered an essential ability in
Read More

by Harish K

29 Feb 2024

Functions of Operating System: Features, Uses, Types
5132
The operating system (OS) stands as a crucial component that facilitates the interaction between software and hardware in computer systems. It serves
Read More

by Geetika Mathur

29 Feb 2024

What is Information Technology? Definition and Examples
5058
Information technology includes every digital action that happens within an organization. Everything from running software on your system and organizi
Read More

by spandita hati

29 Feb 2024

50 Networking Interview Questions &#038; Answers (Freshers &#038; Experienced)
5138
In the vast landscape of technology, computer networks serve as the vital infrastructure that underpins modern connectivity.  Understanding the core p
Read More

by Harish K

29 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon