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?
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 How do I uninstall a package installed using npm link?