How to update dependency in package.json file ?
Last Updated :
27 Feb, 2020
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
Similar Reads
How to add a non-npm dependency to package.json?
One of the great features of the npm ecosystem is the ability to install and manage packages from the npm registry. These dependencies are listed in the "dependencies" section of the project's package.json file. Â However, sometimes you may need to use a dependency that isn't available through npm, s
5 min read
How to Update Data in JSON File using Node?
To update data in JSON file using Node.js, we can use the require module to directly import the JSON file. Updating data in a JSON file involves reading the file and then making the necessary changes to the JSON object and writing the updated data back to the file. Table of ContentUsing require() Me
4 min read
How to Upgrade NPM Dependencies?
Upgrading NPM dependencies is important to ensure your NodeJS project is updated with the latest features, bug fixes, and security patches This process guarantees compatibility with modern JavaScript environments and increases performance and stability for your projects.NPM (Node Package Manager) is
3 min read
How to Update Local Package in NPM?
Updating the local packages in NPM is a common task for the developers. Whether it is for bug fixes, new features, or security patches. Keeping your dependencies up to date is essential. These are the following approaches to updating the local package in NPM:Table of ContentUpdate to the Latest Stab
4 min read
Understanding & Managing Dependencies in package.json File
In the world of modern web development, managing dependencies is a crucial aspect of building robust and efficient applications. Whether you are working on a small personal project or a large-scale enterprise application, understanding and effectively managing dependencies is essential for maintaini
4 min read
How To Manage Dependencies in Git?
Managing dependencies is very important for software development, ensuring that your project runs smoothly and is maintainable over time. In Git, managing these dependencies effectively can prevent conflicts, reduce errors, and simplify collaboration within your team. In this article, we'll explore
6 min read
How to add Dependency in Scala?
Scala, a strong language that unites both object-oriented and functional programming techniques has to depend on external libraries to enhance its capabilities. To develop projects seamlessly, there must be efficient handling of these libraries. This is where dependency management becomes important.
4 min read
Difference between package.json and package-lock.json files
In Node, package.json file contains the list of dependencies and scripts in a project while the package.lock.json specifies their respective versions to ensure consistent installations in different environments.In this article, we will learn the major differences between package.json and package-loc
4 min read
Updating dependencies in Python Poetry
Updating dependencies in Python Poetry is straightforward and efficient, thanks to its comprehensive command-line interface and powerful dependency resolution capabilities. Regularly updating your dependencies ensures that your project benefits from the latest features, bug fixes, and security impro
3 min read
How to Force an NPM Package to Install?
Forcing an NPM package to install can be necessary in cases where the dependencies of a package are in conflict or when you need to override existing constraints or force the installation of a specific version. Forcing an NPM package to install refers to using specific commands to bypass version con
3 min read