为什么在向根工作区 package.json 添加依赖项时纱线会发出警告 [英] why does yarn warn when adding a dependency to the root workspaces package.json

查看:21
本文介绍了为什么在向根工作区 package.json 添加依赖项时纱线会发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我向工作区项目的根目录添加依赖项时:

Whenever I add a dependency to the root of the workspaces project:

例如

yarn add assets-webpack-plugin -D

我收到以下错误:

运行此命令会将依赖项添加到工作区根目录而不是工作区本身,这可能不是您想要的 - 如果您真的想这样做,请使用 -W 标志再次运行此命令(或 --ignore-workspace-root-check).

Running this command will add the dependency to the workspace root rather than the workspace itself, which might not be what you want - if you really meant it, make it explicit by running this command again with the -W flag (or --ignore-workspace-root-check).

另一种方法是将它添加到每个需要它的项目中,然后就会出现每个项目都有不同依赖项和锁定文件的问题.

The alternative is to add it to every project that needs it and then it will you have the problem of each project having different dependencies and lock files.

推荐答案

既然您正在使用 Yarn Workspaces 并且它管理所有项目(工作区)的依赖项,你应该将每个项目的依赖项添加到它自己的 package.json 中,而不是工作区根.Yarn 仅使用位于工作区根目录中的一个 yarn.lock 文件.此外,它尝试将所有项目的依赖项移动到工作区根目录的 node_modules 以尽可能防止重复.虽然有些依赖需要放在自己项目的node_modules中;例如当工作区根目录有一个 devDependencyawesome-package@2.1.1 而一个项目依赖于另一个版本的同一个包时,比如 1.2.5 它们不兼容.假设您的工作区的目录结构如下所示:

Since you are using Yarn Workspaces and it manages the dependencies of all projects (workspaces), you should add dependencies of each project to its own package.json, not the workspace root. Yarn uses only one yarn.lock file that is placed in the workspace root. Also, it tries to move dependencies of all projects to node_modules of workspace root to prevent duplication as much as possible. Although some dependencies need to be placed in node_modules of their own project; e.g. when the workspace root has a devDependency to awesome-package@2.1.1 while a project has a dependency to the same package with another version, say 1.2.5 which they are not compatible. Suppose the directory structure of your workspaces is like the following:

├── workspace-root
|   ├── package.json
|   ├── workspace-a
|   |   ├── package.json
|   ├── workspace-b
|   |   ├── package.json

在工作区根目录或任何工作区目录中运行 yarn 后,您将拥有以下目录结构:

After running yarn either in the workspace root or in any workspace directory, you will have the following directory structure:

├── workspace-root
|   ├── node_modules
|   ├── package.json
|   ├── yarn.lock
|   ├── workspace-a
|   |   ├── package.json
|   |   ├── node_modules
|   ├── workspace-b
|   |   ├── package.json
|   |   ├── node_modules

仅当您想从工作区根运行脚本并且需要依赖项时才向工作区根添加依赖项.在这种情况下,项目独立于该依赖项,因此您可以忽略该警告.

Only add a dependency to workspace root when you want to run a script from workspace root and it needs a dependency. In this case, the projects are independent of that dependency, so you can ignore that warning.

如果将项目的公共依赖添加到工作空间根目录中,则不会出现在项目的package.json中.因此,如果你分离一个项目,它不会在它自己的 package.json 中拥有它的所有依赖项,所以为分离的项目运行 yarn install 会导致没有所有的依赖node_modules 中的依赖项.显然,分离的项目无法工作,您需要修复缺少依赖项的问题才能解决问题.

If you add the common dependencies of the projects to the workspace root, it won't come in package.json of the projects. Therefore, if you separate a project, it won't have all of its dependencies in its own package.json so running yarn install for the separated project leads to not having all the dependencies in its own node_modules. Obviously the separated project cannot work and you need to fix the absent dependencies problem to resolve the issue.

Yarn Workspaces 是一项功能,旨在更轻松地管理相互关联的项目的依赖项.例如,当您的项目具有相似的依赖项时,您可以将每个项目声明为一个工作区.它可以防止大量重复.另一个重要的用例是 monorepos:

Yarn Workspaces is a feature for the sake of easier managing dependencies of projects that are related to each other. For example, when your projects have similar dependencies, you can declare each project as a workspace. It prevents a lot of duplication. Another important use case is monorepos:

尝试将项目拆分为多个包的人都知道一次跨多个包进行更改是多么困难.为了简化流程,一些大型项目采用了 monorepo方法,或多包存储库,这减少了负担跨包编写代码.

Those who have tried splitting a project into multiple packages know how hard it is to make changes across multiple packages at one time. To make the process easier, some big projects adopted a monorepo approach, or multi-package repositories, which reduces the burden of writing code across packages.

JavaScript 开发者每天使用的几个项目都被管理作为 monorepos:Babel、React、Jest、Vue、Angular.

Several projects used every day by JavaScript developers are managed as monorepos: Babel, React, Jest, Vue, Angular.

使用 Yarn Workspaces 带来以下好处:

Using Yarn Workspaces brings the following benefits:

  • 它允许您以这样的方式设置多个软件包,您只需运行一次 yarn install 即可将所有软件包安装在一个单程.
  • 您的依赖项可以链接在一起,这意味着您的工作区可以相互依赖,同时始终使用可用的最新代码.

  • It allows you to setup multiple packages in such a way that you only need to run yarn install once to install all of them in a single pass.
  • Your dependencies can be linked together, which means that your workspaces can depend on one another while always using the most up-to-date code available.

这也是比 yarn link 更好的机制,因为它只影响您的工作区树而不是整个系统.

This is also a better mechanism than yarn link since it only affects your workspace tree rather than your whole system.

您的所有项目依赖项都将安装在一起,从而为 Yarn 提供更多空间来更好地优化它们.

All your project dependencies will be installed together, giving Yarn more latitude to better optimize them.

Yarn 将为每个项目使用一个锁文件而不是不同的锁文件,这意味着更少的冲突和更容易的审查.

Yarn will use a single lockfile rather than a different one for each project, which means fewer conflicts and easier reviews.

这篇关于为什么在向根工作区 package.json 添加依赖项时纱线会发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆