How To Reinstall Packages With Npm

Glory Kim
Glory KimSoftware Engineer, Loom
Published: May 20, 2024

Reinstall all npm packages

To force this behavior, start by deleting the node\_modules directory within the project:

Bash
$ rm -rf node_modules

Then run the install command to reinstall the packages in a new node\_modules folder:

Bash
$ 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:

Bash
$ 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:

Bash
$ 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.

Bash
$ 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
Glory KimSoftware Engineer, Loom
Filed under

Related articles


List Installed Npm Packages

Learn how to list globally and locally installed packages with npm, including their dependencies.

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

Clear npm Cache

Learn how to clear the npm cache on Linux, macOS, and Windows.

Install Dev Dependencies With npm

Learn how to install and update dev dependencies using the npm install command.

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.

Re-Installing Npm

Learn how to reinstall Node.js and npm on macOS, Linux, and Windows using `curl`, `brew`, `apt`, `nvm`, and Node installer.

How To Update NPM

Learn how to update npm to a specific version using the npm, nvm, or npx commands.