112

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?

Aurora0001
  • 13,139
  • 5
  • 50
  • 53
Kevin
  • 14,655
  • 24
  • 74
  • 124
  • I had this conversation in chat about this question: http://chat.stackoverflow.com/rooms/17/conversation/reinstalling-local-dependency-in-yarn The advice I got in this chat sometimes works, but I have a hard time consistently updating my local dependency. – Kevin Jan 26 '17 at 00: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 '17 at 02:33
  • @EzraChang As I discussed in my question, I've tried that and it hasn't worked. – Kevin Jan 26 '17 at 17:39

8 Answers8

188

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

yarn install --check-files

Kevin
  • 14,655
  • 24
  • 74
  • 124
Karl Adler
  • 15,780
  • 10
  • 70
  • 88
  • 17
    It doesn't when the source is a git repo – Jaime Agudo May 11 '18 at 16:12
  • I'm not sure what you mean...usually the source is a git repo and it works. – Karl Adler May 14 '18 at 12:27
  • 2
    @abimelex I think Jaime means a reference in package.json like this: `"dependencies" : { "project" : "user/project#branch-name" }`. – Christiaan Westerbeek Jun 05 '18 at 06:53
  • Just, what I needed now! Thanks :] – Thomas Junk Sep 07 '18 at 07:15
  • This fixed my "Error: Cannot find module '../lib/completion'" and similar issues prob caused by mega sync! Thank you! – Neoraptor May 24 '19 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 07 '21 at 04:50
  • can anyone tell what the solution for this is in yarn 3 with pnp instead of node modules ? – SuperSaiyan99000 Jun 03 '23 at 11:41
  • It took a bit to re-install the one package I deleted from modules. I assume --check-files checks all the folders. – Panos Roditakis Aug 11 '23 at 09: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?

Kevin
  • 14,655
  • 24
  • 74
  • 124
40

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

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

Sergey Okatov
  • 1,270
  • 16
  • 19
32

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
Ilia Liachin
  • 1,284
  • 2
  • 17
  • 35
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.
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
7

Try:

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

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

Junior Tour
  • 439
  • 7
  • 8
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
xenoterracide
  • 16,274
  • 24
  • 118
  • 243
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.

Eric Wiener
  • 4,929
  • 4
  • 31
  • 40