121

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?

3
  • I had this conversation in chat about this question: chat.stackoverflow.com/rooms/17/conversation/… The advice I got in this chat sometimes works, but I have a hard time consistently updating my local dependency.
    – Kevin
    Jan 26, 2017 at 0:22
  • Remove your node_modules directory, update your package.json with the proper version, and reinstall everything. Yarn is fast; it'll only take you a few seconds.
    – Ezra Chang
    Jan 26, 2017 at 2:33
  • @EzraChang As I discussed in my question, I've tried that and it hasn't worked.
    – Kevin
    Jan 26, 2017 at 17:39

8 Answers 8

195

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

yarn install --check-files

7
  • 17
    It doesn't when the source is a git repo May 11, 2018 at 16:12
  • I'm not sure what you mean...usually the source is a git repo and it works.
    – Karl Adler
    May 14, 2018 at 12:27
  • 2
    @abimelex I think Jaime means a reference in package.json like this: "dependencies" : { "project" : "user/project#branch-name" }. Jun 5, 2018 at 6:53
  • This fixed my "Error: Cannot find module '../lib/completion'" and similar issues prob caused by mega sync! Thank you!
    – Neoraptor
    May 24, 2019 at 12:17
  • 3
    For Yarn 2, I thought the equivalent command might be yarn install --check-cache, but it seems to not be, since it didn't work to force a package reinstall (for the ones I tested anyway). For now, I'm using the workaround of changing the dependency's version in package.json, running yarn, then changing it back, and running yarn again.
    – Venryx
    Aug 7, 2021 at 4:50
44

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

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

2
  • 2
    This may have unintended effects as it will update the package to the latest version within the range specified in package.json.
    – Peter Cole
    Jul 26, 2022 at 9:46
  • If you want to just update the existing package with it's existing version, this won't work. May 16 at 10:52
42

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?

0
34

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
17

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.
7

Try:

  1. yarn cache clean [<module_name...>]

  2. yarn add [<module_name...>]

4

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
3

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.