How do I force Yarn to reinstall a package?

node.jsYarnpkg

node.js Problem Overview


My project has a dependency that I sometimes get from a package server and sometimes get from a local copy I have on my machine. As a result, I frequently need to have Yarn switch where it looks for the dependency. Furthermore, I often change the local copy of the dependency and need to see that change reflected in my main project. As a result, I need a way to tell Yarn to continue looking at the same location for the dependency, but to reinstall the dependency, skipping the cache and grabbing it directly from its current source, even when the version number hasn't changed. (Sometimes I want try small changes to the dependency, and updating the version number every time would quickly become annoying.)

How do I do so?

I've tried the following, but none of them work:

yarn remove dependency
yarn add file:/dependency

Continues to use the previous version of the dependency.

yarn remove dependency
yarn cache clear
yarn add file:/dependency
yarn install --force

Also continues to use the previous version of the dependency.

yarn remove dependency
rm -rf node_modules/
yarn cache clear
yarn add file:/dependency
yarn install --force

Still continues to use the previous version of the dependency.

How can I ensure that Yarn is using the latest version of my dependency?

node.js Solutions


Solution 1 - node.js

Reinstalling a package after just deleting the node module works with:

yarn install --check-files

Solution 2 - node.js

You can use the yarn link command. This will set up your local dependency so that whenever you make a change on the dependency, it immediately shows up in your main project without you having to do anything else to update it.

If your main project is in ~/programming/main and your dependency is in ~/programming/dependency and is named MyLocalDependency, you will want to:

  1. Run yarn link (with no additional flags) from within your dependency:

    cd ~/programming/dependency yarn link

  2. Run yarn link <name of dependency package> from within your main project:

    cd ~/programming/main yarn link MyLocalDependency

And you're done!

If you want to switch from a local copy of the dependency to one hosted elsewhere, you can use yarn unlink.

cd ~/programming/main
yarn unlink MyLocalDependency
cd ~/programming/dependency
yarn unlink

If you're using NPM instead of Yarn, npm link and npm link <dependency> work in effectively the same way. To unlink the dependency, run npm rm --global <dependency>. (This is because npm link works by creating a simlink in the global NPM set of packages, so uninstalling the linked dependency from the global packages also breaks the link.)

See the npm link documentation and https://stackoverflow.com/questions/19094630/how-do-i-uninstall-a-package-installed-using-npm-link

Solution 3 - node.js

There is one other way. Just use yarn upgrade package-name

See manual: https://yarnpkg.com/lang/en/docs/cli/upgrade/

Solution 4 - node.js

As Kevin self-answered, yarn link is a good option.
But it can cause some issues if the package you are linking has peer dependencies.

What Karl Adler said is also a way to go:

yarn --check-files

But this will reinstall (yarn without sub-command is the same as yarn install) every package which has changed.

So, if you really want to just reinstall one package:

yarn add package-name --force

Solution 5 - node.js

Beside these answers, I have a problem with switching git branches and the yarn. I have a branch for updating node_modules packages and another one for my project bug fixing. when I checkout the bug fix and back to updating branch, yarn install or yarn returns:

success Already up-to-date.
✨  Done in 0.79s.

But all new packages are not installed. so with the below command, I forced yarn to install all packages:

yarn --check-files

And now it returns:

🔨  Building fresh packages...
✨  Done in 79.91s.

Solution 6 - node.js

Although this isn't a Yarn answer (it does seem to work fine with yarn, no package.lock or anything), this is what I ended up doing for cypress (cypress puts files where imho, it shouldn't, and if you're caching node_modules in CI... Leaving this answer in case someone else has a similar problem to me, and finds this post.

npm rebuild cypress

Solution 7 - node.js

Try:

  1. yarn cache clean []

  2. yarn add []

Solution 8 - node.js

In case you were like me and were installing one of your personal packages (no one else had access) that you rebased and then force pushed to git, and received the error:

$ yarn add https://github.com/username/my-rebased-package.git
error Command failed.
Exit code: 128
Command: git
Arguments: pull
Directory: /Users/eric/Library/Caches/Yarn/v6/.tmp/8ebab1a3de712aa3968f3de5d312545b
Output:
error: Pulling is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

You can solve this by just directly removing the cached folder:

$rm -rf /Users/eric/Library/Caches/Yarn/v6/.tmp/8ebab1a3de712aa3968f3de5d312545b

You can then install no problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevinView Question on Stackoverflow
Solution 1 - node.jsKarl AdlerView Answer on Stackoverflow
Solution 2 - node.jsKevinView Answer on Stackoverflow
Solution 3 - node.jsSergey OkatovView Answer on Stackoverflow
Solution 4 - node.jsIlia LiachinView Answer on Stackoverflow
Solution 5 - node.jsAmerllicAView Answer on Stackoverflow
Solution 6 - node.jsxenoterracideView Answer on Stackoverflow
Solution 7 - node.jsJunior TourView Answer on Stackoverflow
Solution 8 - node.jsEric WienerView Answer on Stackoverflow