As we have seen, the main function of NPM is to install and manage the packages and dependencies of a project. A very important part of management is updating the version packages.
NPM provides a simple way (within reason) to update the installed packages in our project. To update a specific package, we simply have to use the following command:
npm update package-name
This command searches for the latest version of the specified package and updates it in our project.
If no package name is specified, NPM will update all packages to their latest versions.
npm update package-name
For package updates, NPM takes into account the versions we have specified in our package.json
file. NPM will only update the package if the new version meets the established version requirements.
Uninstalling packages
Sometimes, we may need to uninstall a package from our project. NPM provides us with a simple way (again, within reason) to do this using the uninstall
or remove
command.
To uninstall a specific package, we can use the following command:
npm uninstall package-name
This command will remove the specified package from our project. It is also possible to uninstall multiple packages at the same time by simply specifying the package names separated by spaces.
When uninstalling a package, all dependencies that the package had in our project will also be removed.
Cleaning up unused packages
If we have installed packages that no longer appear in the package.json
file, we can remove them with the command.
npm prune
The npm prune
command searches for and removes any packages that are not present in the dependency tree specified in package.json
.
Additional considerations
Updating packages is essential to maintain the security and performance of the project. You need to update packages and dependencies, that’s a fact. Package updates may include new features or patches for security vulnerabilities. Therefore, it is important to update regularly.
But, there is also a risk that updates may introduce changes that affect existing functionality. This could lead to failures in your project and well… kaboom 💥.
Therefore, it is crucial to conduct thorough testing before updating and to have a backup plan in case of issues during the process. For example, making sure to commit to source control always before updating or uninstalling a package.