Terminus
How To Reinstall Packages With Npm

How To Reinstall Packages With Npm

[#reinstall-all-npm-packages]Reinstall all npm packages[#reinstall-all-npm-packages]

To force this behavior, start by deleting the [.inline-code]node_modules[.inline-code] directory within the project:

 $ rm -rf node_modules

Then run the [.inline-code]install[.inline-code] command to reinstall the packages in a new node_modules folder:

 $ npm install

The [.inline-code]install[.inline-code] command creates the [.inline-code]node_modules[.inline-code] within your current directory and downloads the package to the project.

[#reinstall-single-npm-package]Reinstall a single npm package[#reinstall-single-npm-package]

Npm searches for a [.inline-code]package.json[.inline-code] 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: [.inline-code]npm uninstall <package-name>[.inline-code]
  2. Install the package: [.inline-code]npm install <package-name>[.inline-code]

The [.inline-code]uninstall[.inline-code] command removes the package from the [.inline-code]node_modules[.inline-code] and removes it from [.inline-code]dependencies[.inline-code], [.inline-code]devDependencies[.inline-code], [.inline-code]optionalDependencies[.inline-code], and [.inline-code]peerDependencies[.inline-code] within the `package.json` file. To keep the package within the [.inline-code]package.json[.inline-code] file, you can append the [.inline-code]--no-save[.inline-code] flag to the [.inline-code]uninstall[.inline-code] command like so:

 $ npm uninstall --no-save 

[#reinstall-npm-package-globally]Reinstall an npm package globally[#reinstall-npm-package-globally]

Uninstalling a global package requires the [.inline-code]-g[.inline-code] flag and follows the same steps as above:

  1. Uninstall the package globally [.inline-code]npm uninstall -g <package-name>[.inline-code]
  2. Install the package globally [.inline-code]npm install -g <package-name>[.inline-code]

[#updating-npm-packages]Updating npm packages[#updating-npm-packages]

If you’re looking to download the latest versions of your packages, [.inline-code]npm update[.inline-code] 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]Alternative methods of reinstalling[#alternative-methods-of-reinstalling]

[#npm-reinstall]Using a third party library - [.inline-code]npm-reinstall[.inline-code][#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-npm-cache]Clearing your npm cache to invalidate older versions[#clearing-npm-cache]

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.