How do I force Yarn to reinstall a package?
Emily Wong
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:/dependencyContinues to use the previous version of the dependency.
yarn remove dependency
yarn cache clear
yarn add file:/dependency
yarn install --forceAlso 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 --forceStill continues to use the previous version of the dependency.
How can I ensure that Yarn is using the latest version of my dependency?
38 Answers
Reinstalling a package after just deleting the node module works with:
yarn install --check-files
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 link2) Run yarn link <name of dependency package> from within your main project:
cd ~/programming/main
yarn link MyLocalDependencyAnd 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 unlinkIf 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 How do I uninstall a package installed using npm link?
There is one other way.
Just use yarn upgrade package-name
See manual:
0As 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-filesBut 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 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-filesAnd now it returns:
🔨 Building fresh packages...
✨ Done in 79.91s. 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 Try:
yarn add [<module_name...>]
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
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 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/8ebab1a3de712aa3968f3de5d312545bYou can then install no problem.
0