Open In App

How to update dependency in package.json file ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to update the dependencies of a project with npm. You must have heard about npm which is called a node package manager. So, we can run this command to install an npm package.

npm install

Note: The –save flag is no longer needed after the Node 5.0.0 version.

The package gets installed and should be found in the node_modules folder in the same directory where package.json file exists. The entry of the package name with version should be found in package.json and package-lock.json. You can see the current version of the package in package.json file. You can find the latest version of the npm added in package.json file.

If you want to add a specific version you can run npm install @version_here like npm install react@^1.8.5

{
    "dependencies": {
        "react": "^1.8.5"
    }
}

If you want to add the latest version either you can run npm install or npm install @latest.

{
    "dependencies": {
        "react": "^16.12.0"
    }
}

By looking at those two files we got clear that we have version 16.12.0 of react and the rule for future updates is ^. This rule means that in future npm can only update to patch and minor releases.

In the future, if there will be a new patch or minor release and when we type the npm update command, the already installed version will get updated and package-lock.json will also get updated simultaneously by getting filled with the new version. It is clear that only package-lock.json file will get updated but not package.json.

To get the whole list of the new releases or outdated packages till now, we have to run this command

npm outdated

By running npm update it will not update the version of the major releases because major releases are never updated in this way because some of them might introduce the breaking changes in your live web app so npm don’t want to put in trouble.

For updating a new and major version of the packages, you must install the npm-check-updates package globally.

npm install -g npm-check-updates

After installing the package run the following command:

ncu

It will display the new dependencies in the current directory whereas running this command will list all the global packages which have new releases.

ncu -g

Now run this command:

ncu -u

After running this command it will result in the upgrading of all the version hints in the package.json file, so npm will install the major version by using this method.

Now everything is done. Just run the update command:

npm update

If you have a new project without any node_modules dependencies and you want to install the new version then run the following command:

npm install

Last Updated : 27 Feb, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads