Open In App

How to Update Local Package in NPM?

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Screenshot-2024-10-14-175041

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:

Screenshot-2024-10-14-175318

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]
Screenshot-2024-10-14-180023

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:

Screenshot-2024-10-14-181323
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
Screenshot-2024-10-14-182122
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.


Next Article

Similar Reads