How to force Yarn to reinstall a package [5 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
3 min

banner

# Table of Contents

  1. How to force Yarn to reinstall a package
  2. Using the yarn upgrade command
  3. Delete your node_modules directory and your yarn.lock

# How to force Yarn to reinstall a package

To force yarn to reinstall a package:

  1. Delete your node_modules directory.
shell
# on macOS and Linux rm -rf node_modules # on Windows (CMD) rd /s /q "node_modules"

delete node modules

  1. Clear your yarn cache.
shell
yarn cache clean

clear yarn cache

  1. Run the yarn install command with the --check-files flag.
shell
yarn install --check-files

yarn install check files

When the --check-files is set, yarn verifies that already installed files in node_modules haven't been removed.

If that doesn't work, call the yarn add command with the --force flag.

shell
yarn add <package-name> --force

force yarn to reinstall all packages

Make sure to replace the <package-name> placeholder with the name of the package you want to reinstall.

You can also run the yarn install --force command to force reinstall all packages.

shell
yarn install --force

yarn force reinstall all packages

# Using the yarn upgrade command

If you still weren't able to force yarn to reinstall the package, use the yarn upgrade command.

The command updates your dependencies to the latest version based on the version range that is specified in your package.json file.

The command also recreates your yarn.lock file.

You can use the command with a specific package.

shell
# force yarn to reinstall a specific package yarn upgrade <package-name>

yarn upgrade package

Make sure to replace <package-name> with the name of the package you want to reinstall, e.g. yarn upgrade axios.

Or use the command to update all dependencies in your package.json file.

shell
# force yarn to reinstall all packages yarn upgrade

force yarn to reinstall all packages

If the issue persists, try to rebuild the package by issuing the following command.

shell
npm rebuild <package-name>

rebuild package

The npm rebuild command is useful when you install a new version of node and need to recompile your C++ addons with the new binary.

# Delete your node_modules directory and your yarn.lock

If the issue persists, you can try to delete your node_modules directory and your yarn.lock file and reinstall your dependencies.

If you are on macOS or Linux, run the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages yarn install

If you are on Windows, run the following commands in CMD (Command Prompt).

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages yarn install

You can also try to run the yarn install command with the --force flag.

shell
yarn install --force

yarn force reinstall all packages

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev