How to Update Local Package in NPM?
Last Updated :
15 Oct, 2024
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:
Steps to check and update the local package in npm
Step 1: Check the Current Version of Your Package
Before updating the package, knowing which version is currently installed is more important. You can do this by running the following command in the root directory of your project:
npm list <package-name>
- The above command will display the version of the currently installed package.
- For example, if you are using a package called Winston, you can check its version like this:
npm list winston
Output:
Update to the Latest Stable Version
To update the package to the latest version you can use the following command:
npm update <package-name>
- This command updates the specified package to the latest version compatible with the server (semantic versioning) ranges defined in your package.json.
- For example, to update Winston:
npm update winston
Output:
Update to Specific Version
If you want to update to specific version (e.g., for compatibility reasons) you can specify version number like this:
npm install <package-name>@<version>
For example to install version 4.17.1 of winston, you would run:
npm install [email protected]
This will install the exact version and update the package.json file to reflect that version.
Step 3: Update package.json File
- Whenever you update the package, it is important that your package.json file is kept in sync. If you ran npm install <package-name> command, it would automatically update package.json file to reflect new version.
- In package.json file, the dependencies are listed like this:
{
"dependencies": {
"winston": "^3.8.2"
}
}
- The ^ symbol before version number allows the minor updates but locks down the major version. So, if the minor version update is available (e.g., from 4.17.1 to 4.18.0) it will automatically be installed when you run the npm update.
- If you want to allow both minor and the major updates, you can use * or manually can change the version range:
{
"dependencies": {
"winston": "*"
}
}
This allows the NPM to install any version of winston package.
Step 4: Verify Update
After updating, you should verify that the package has been updated correctly. Run following command to list the installed version of your packages:
npm list <package-name>
Alternatively, you can run following to see the complete list of all installed packages and their versions:
npm list
Output:
here having only 1 package- Above output showing that as I updated to specific version from 3.15.0 to 3.8.2 .
- Make sure that updated package version appears in the list.
Step 5: Test Your Application
- Whenever you update the package, it is more important to test your application to ensure everything works as expected. Sometimes, the updates might introduce breaking changes, especially when moving between the major versions.
- Run your application and test functionality that relies on the updated package. If something is broken you may need to consult package release notes to see what changes were made and have to adjust your code accordingly.
Updating All Dependencies at Once
If you want to update all packages in your project to their latest versions (within range specified in the package.json), you can use this command:
npm update
as it changed from 3.8..2 to 3.15.0- This will scan your package.json file, check for the newer versions of your dependencies and will update them.
Updating Global Packages
- Sometimes you might have global packages that need to be updated (like tools you install globally such as npm, gulp or the webpack). To update the global package, you use the -g flag with install command:
npm install -g <package-name>
- For example to update npm globally
npm install -g http-server
Conclusion
Locking the Versions: If you want to lock your package versions so they do not automatically update, use the exact version number in package.json (e.g., "express": "4.17.1"). Keeping the Dependencies Clean: You can run npm prune to remove any packages that are no longer being used in your project.
Similar Reads
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
How to update dependency in package.json file ?
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
3 min read
How to Install GIT by NPM Packages?
Git is a library for git written in Node.js which allows for the manipulation of git repositories by the Node.js application. Git (npm), not to be confused with GIT, the version control software is a Node.js port of the now-defunct Git project. It is fairly synchronous as-is, and it allows the devel
2 min read
Top npm packages for node
NodeJS has become an effective tool for server-side development, thanks to its extensive ecosystem of npm packages. These packages offer a wide range of functionalities, from web frameworks to utility libraries, enabling users to build robust and scalable applications efficiently. In this article, w
4 min read
How to Update NPM?
NPM (Node Package Manager) is an essential tool for managing JavaScript libraries and packages. Itâs commonly used for NodeJS projects to install, update, and manage dependencies. Like other software, NPM also gets updated, which introduces new features, improvements, and bug fixes. NPM can install
4 min read
Where does NPM Install the packages ?
NPM is the default package manager for Node.js , and it is used to install, manage, and distribute JavaScript packages. When you add a package using NPM install, the location of the installed package depends upon whether the package is installed globally or locally. Table of Content Local Installati
3 min read
How to list npm user-installed packages in Node.js?
What is Node.js? Node.js is an open source and cross-platform runtime environment for executing JavaScript code outside of a browser. Click here for more. What is npm? Here, "npm" stands for "Node Package Manager" which is the package manager for Node.js and serves as a command-line utility for inte
2 min read
How To Use Node Modules with npm and package.json
NodeJS is a powerful runtime for server-side JavaScript & these modules are reusable pieces of code that can be easily imported and used in NodeJS applications. npm (Node Package Manager) is the default package manager for Node JS and is used to install, manage, and publish NodeJS packages. This
3 min read
How to Find the Version of Installed NPM Package?
Knowing the version of NPM installed on your system is important especially when working with specific versions of Node JS or when troubleshooting issues. This article provides instructions to check the version of NPM installed on your system. Prerequisites:Node JS Command Line InterfaceSteps To Fin
1 min read
How to document NPM packages ?
In this article, we will see how to write the documentation of an NPM package. Documentation is an essential part of any NPM package because it gives an idea about the package method and how to use them. Good documentation makes your npm package popular npm packages. The Documentation of the npm pac
2 min read