How To Reinstall Packages With Npm
Glory Kim
Software Engineer, Loom
Published: 5/20/2024
Reinstall all npm packages
To force this behavior, start by deleting the node_modules directory within the project:
$ rm -rf node_modules
Then run the install command to reinstall the packages in a new node_modules folder:
$ npm install
The install command creates the node_modules within your current directory and downloads the package to the project.
Reinstall a single npm package
Npm searches for a package.json file that specifies the versions of the packages to download. If a package does not exist within this file, it will be installed to its latest version. To install a package at a specific version, append the version number:
$ npm install@
- 1. Uninstall the package: npm uninstall <package-name>
- 2. Install the package: npm install <package-name>
The uninstall command removes the package from the node_modules and removes it from dependencies, devDependencies, optionalDependencies, and peerDependencies within the package.json
file. To keep the package within the package.json file, you can append the --no-save flag to the uninstall command like so:
$ npm uninstall --no-save
Reinstall an npm package globally
Uninstalling a global package requires the -g flag and follows the same steps as above:
- 1. Uninstall the package globally npm uninstall -g <package-name>
- 2. Install the package globally npm install -g <package-name>
Updating npm packages
If you’re looking to download the latest versions of your packages, npm update is for you. This command will update all packages to the latest versions of the set semver constraints. Refer to the documentation for more detail.
When running this command within a project, you can specify to update a specific package by adding the package name.
$ npm update @
Alternative methods of reinstalling
Using a third party library - npm-reinstall
There are several third party libraries that aims to simplify the re-installation process of npm packages such as npm-reinstall. The benefit of this library is that it only requires one command instead of two. However, this library has not been updated since 2019 and can cause noisy consoles and warnings upon usage. It also requires several dependencies that may be unnecessary. Instead, we recommend developers use the commands recommended by npm.
Clearing your npm cache to invalidate older versions
It is typically unnecessary to clear your npm cache when trying to reinstall npm packages. Npm caches data related to http request data and other package-related information. As more packages get added to your project, this cache will grow. Npm has set up a fail-safe where data is fetched automatically to combat cache corruption.
For this reason, cleaning out npm’s cache with npm cache clean is unnecessary to solve installation problems unless you’re trying to reclaim disk space. Instead, refer to the solutions above and use npm's own suggestion of reinstalling.
Written by
Glory Kim
Software Engineer, Loom
Filed Under
Related Articles
Removing npm Packages
Learn how to remove packages locally, globally, and from the registry using the npm uninstall command.
Execute Packages With npx And npm
Discover the power of npm's npx tool, a developer's best friend for running packages without global installs.
Install Dev Dependencies With npm
Learn how to install and update dev dependencies using the npm install command.
Clear npm Cache
Learn how to clear the npm cache on Linux, macOS, and Windows.
How To Update NPM
Learn how to update npm to a specific version using the npm, nvm, or npx commands.
Re-Installing Npm
Learn how to reinstall Node.js and npm on macOS, Linux, and Windows using `curl`, `brew`, `apt`, `nvm`, and Node installer.
Check Npm Package Version
Check an npm package version within your project
Install NPM Packages From GitHub
Check an npm package version within your project
List Installed Npm Packages
Learn how to list globally and locally installed packages with npm, including their dependencies.