Managing Packages with NPM: How to use the package JSON file

Enter the Node Package Manager

Managing packages is a major aspect of the backend development process because it allows you to keep track of your software, update it, and upgrade them where necessary. For Node, we used NPM (node package manager) to manage packages and libraries which your node applications need to run successfully.

In this tutorial, we will learn how to use a Package.Json file.

How to use Package JSON

Package.JSON is the most important part of any Node.js project because it contains important specifications and requirements for your program.

Just as the human brain serves as the control center of the body, package.json functions in the same way by acting as the control point of your node projects. A simple package.json file will look like the one below.

{

“name”: “New project”,

“version”: “1.0.0”,

“description”: “A simple Node.js project”,

},

“dependencies”: {

“Express”: “^4.18.2”,

},

“devDependencies”: {

“mocha”: “^10.2.0”,

“chai”: “^4.3.7”

},

“author”: “FishonsNote”,

“license”: “MIT”

}

Understanding key components of a Package.Json File

Package.json has some key components. We will explain all of them below in order of importance.

Dependencies

Dependencies form the most essential components of your package.Json file. We can compare dependencies to recipes used to make cakes. To bake a cake, you need flour, sugar, and an egg.

So all also for your node project to work, you must specify and use essential dependencies. For example, if you are building an application using an express framework, you must specify the express package as a dependency if not your web app with not work.

Dependencies are codes that your project references when it is running. If these codes are missing or outdated, your product is bound to have issues.

The code below shows how to specify a dependency.

“dependencies”: {

“Express”: “~4.18.2”,

The first item specifies the name of the dependency, while the second item specifies the version of the dependency. Make sure you install and use the latest versions of each dependency to avoid issues.

Dependency versions

Dependency versioning is managing versions of packages or libraries that node.js projects references during runtime. The package.json file is where all packages in versioned using the format major, minor, and patch formats.

From your package.json file, you can specify the specific updates type your package can receive. Let us specify a package version range up to 1.0.3 below.

Patch: 1.0 or 1.0.x or ~1.0.3:

Minor update: 1 or 1. x or ^1.0.3

Major updates: * or x

DevDependencies

Not all dependencies are shipped to production. That is what dev dependencies are. Because devdependecies simply help programmers to test and execute programs locally, they are not needed in the final product. Devdependencies are listed in a separate section in the package.json file.

One of the most popular devdependencies used in many node projects is the webpack. Webpack helps in bundling node files together so they can run efficiently in a browser. However, it is not useful post-production and is mostly discarded after the project is shipped.

Other components of a package.json

Version: Each update either major, minor, or patch upgrades your project version level.

Author: This is the owner of the project.

License: This refers to the type of project license. The most popular type is the MIT open license.

In conclusion, package.json represents the core of any node application. Therefore, understanding how to manage this package.json is an essential skill required to create and deploy successful applications.