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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41864099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:57:09  来源:igfitidea点击:

How do I force Yarn to reinstall a package?

node.jsyarnpkg

提问by Kevin

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

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

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?

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

采纳答案by Kevin

You can use the yarn linkcommand. 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.

您可以使用该yarn link命令。这将设置您的本地依赖项,以便每当您对依赖项进行更改时,它会立即显示在您的主项目中,而您无需执行任何其他操作来更新它。

If your main project is in ~/programming/mainand your dependency is in ~/programming/dependencyand is named MyLocalDependency, you will want to:

如果您的主项目在~/programming/main并且您的依赖项在~/programming/dependency并命名为MyLocalDependency,您将需要:

1) Run yarn link(with no additional flags) from within your dependency:

1)yarn link从您的依赖项中运行(没有额外的标志):

cd ~/programming/dependency
yarn link

2) Run yarn link <name of dependency package>from within your main project:

2)yarn link <name of dependency package>从你的主项目中运行:

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.

如果您想从依赖项的本地副本切换到其他地方托管的副本,您可以使用yarn unlink.

cd ~/programming/main
yarn unlink MyLocalDependency
cd ~/programming/dependency
yarn unlink


If you're using NPM instead of Yarn, npm linkand npm link <dependency>work in effectively the same way. To unlink the dependency, run npm rm --global <dependency>. (This is because npm linkworks by creating a simlink in the global NPM set of packages, so uninstalling the linked dependency from the global packages also breaks the link.)

如果您使用 NPM 而不是 Yarn,npm linknpm link <dependency>以相同的方式有效工作。要取消链接依赖项,请运行npm rm --global <dependency>. (这是因为npm link通过在全局 NPM 包集中创建一个 simlink 来工作,因此从全局包中卸载链接的依赖项也会断开链接。)

See the npm linkdocumentationand How do I uninstall a package installed using npm link?

请参阅npm link文件如何卸载使用NPM链接安装包?

回答by Karl Adler

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

删除节点模块后重新安装包适用于:

yarn install --check-files

yarn install --check-files

回答by Sergey Okatov

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

还有另一种方式。只需使用yarn upgrade package-name

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

参见手册:https: //yarnpkg.com/lang/en/docs/cli/upgrade/

回答by seelts

As Kevin self-answered, yarn linkis a good option.
But it can cause some issues if the package you are linking has peerdependencies.

正如凯文自我回答的那样yarn link是一个不错的选择。
但是,如果您链​​接的包具有对等依赖项,则可能会导致一些问题。

What Karl Adler saidis also a way to go:

什么卡尔·阿德勒说,也是很长的路要走:

yarn --check-files

But this will reinstall (yarnwithout sub-command is the same as yarn install) every package which has changed.

但这将重新安装(yarn没有子命令与 相同yarn install)每个已更改的包。

So, if you really want to just reinstall one package:

因此,如果您真的只想重新安装一个软件包:

yarn add package-name --force

回答by AmerllicA

Beside these answers, I have a problem with switching git branches and the yarn. I have a branch for updating node_modulespackages and another one for my project bug fixing. when I checkout the bug fix and back to updating branch, yarn installor yarnreturns:

除了这些答案之外,我在切换 git 分支和yarn. 我有一个用于更新node_modules包的分支和另一个用于修复项目错误的分支。当我检查错误修复并返回更新分支时,yarn installyarn返回:

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 安装所有软件包:

yarn --check-files

And now it returns:

现在它返回:

  Building fresh packages...
?  Done in 79.91s.

回答by xenoterracide

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.

尽管这不是 Yarn 的答案(它似乎对 Yarn 工作正常,没有 package.lock 或任何东西),但这就是我最终为 cypress 所做的(cypress 将文件放在 imho,它不应该的地方,如果你're caching node_modules in CI ... 留下这个答案,以防其他人有与我类似的问题,并找到这篇文章。

npm rebuild cypress

回答by Eric Wiener

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:

如果您像我一样正在安装您重新定位的个人软件包之一(没有其他人可以访问),然后强制推送到 git,并收到错误:

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

然后就可以安装没问题了。