如何强制 Yarn 重新安装软件包?

我的项目有一个依赖项,我有时从包服务器获得,有时从机器上的本地副本获得。因此,我经常需要有纱线开关,它查找依赖关系。此外,我经常更改依赖项的本地副本,并且需要看到这个更改反映在我的主项目中。因此,我需要一种方法来告诉 Yarn 继续查看依赖项的相同位置,但是重新安装依赖项,跳过缓存,直接从当前源中获取它,即使版本号没有改变。(有时我想尝试对依赖项进行一些小的更改,每次更新版本号都会很快变得很烦人。)

我该怎么做?

我试过以下方法,但都不管用:

yarn remove dependency
yarn add file:/dependency

继续使用依赖项的以前版本。

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

也继续使用依赖项的早期版本。

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

仍然继续使用依赖项的以前版本。

如何确保 Yarn 正在使用我的依赖项的最新版本?

167813 次浏览

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?

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

yarn install --check-files

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

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

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

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.

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

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.

Try:

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

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