Programs

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.

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

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)!

What 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.

How do I run Package JSON Scripts?

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

How 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.

What 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.

Want to share this article?

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks