简体   繁体   English

为什么在向根工作区 package.json 添加依赖项时 yarn 会发出警告

[英]why does yarn warn when adding a dependency to the root workspaces package.json

Whenever I add a dependency to the root of the workspaces project:每当我将依赖项添加到工作区项目的根目录时:

eg例如

yarn add assets-webpack-plugin -D

I get the following error:我收到以下错误:

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).运行此命令会将依赖项添加到工作区根而不是工作区本身,这可能不是您想要的 - 如果您真的想要它,请通过使用 -W 标志(或 --ignore-工作区根检查)。

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.另一种方法是将它添加到每个需要它的项目中,然后你就会遇到每个项目都有不同的依赖项和锁定文件的问题。

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工作区,并且它管理所有项目(工作区)的依赖关系,因此应将每个项目的依赖关系添加到其自己的package.json ,而不是工作区根目录中。 Yarn uses only one yarn.lock file that is placed in the workspace root. Yarn仅使用放置在工作区根目录中的一个 yarn.lock文件。 Also, it tries to move dependencies of all projects to node_modules of workspace root to prevent duplication as much as possible. 另外,它尝试将所有项目的依赖项node_modules工作区根目录的node_modules ,以尽可能避免重复。 Although some dependencies need to be placed in node_modules of their own project; 尽管某些依赖项需要放置在其自己项目的node_modules中; eg 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. 例如,当工作空间根目录对awesome-package@2.1.1具有devDependency ,而项目对另一个版本(例如1.2.5的同一软件包具有依赖性时,它们是不兼容的。 Suppose the directory structure of your workspaces is like the following: 假设工作空间的目录结构如下所示:

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

After running yarn either in the workspace root or in any workspace directory, you will have the following directory structure: 在工作空间根目录或任何工作空间目录中运行yarn之后,您将具有以下目录结构:

├── 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. 在这种情况下,项目独立于该依赖关系,因此您可以忽略该警告。

Why does yarn warn? 为什么纱线会发出警告?

If you add the common dependencies of the projects to the workspace root, it won't come in package.json of the projects. 如果将项目的公共依赖项添加到工作区根目录,则不会出现在项目的package.json中。 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 . 因此,如果您分离一个项目,则它不会在其自己的package.json拥有所有依赖关系,因此为分离的项目运行yarn install会导致其自己的node_modules没有所有依赖关系。 Obviously the separated project cannot work and you need to fix the absent dependencies problem to resolve the issue. 显然,分离的项目无法正常工作,您需要修复缺少的依存关系问题才能解决该问题。

More about Yarn Workspaces 有关纱线工作区的更多信息

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 : 另一个重要的用例是monorepos

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. 为了简化过程,一些大型项目采用了monorepo方法或多包存储库,从而减轻了跨包编写代码的负担。

Several projects used every day by JavaScript developers are managed as monorepos: Babel, React, Jest, Vue, Angular. JavaScript开发人员每天都会使用几个项目作为monorepos进行管理:Babel,React,Jest,Vue,Angular。

Using Yarn Workspaces brings the following benefits: 使用纱线工作区带来以下好处:

  • 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. 它允许您以这样一种方式设置多个软件包,您只需运行一次yarn install就可以一次安装所有软件包。
  • 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. 您的依赖关系可以链接在一起,这意味着您的工作空间可以相互依赖,同时始终使用最新的可用代码。

  • This is also a better mechanism than yarn link since it only affects your workspace tree rather than your whole system. 这也是比yarn link更好的机制,因为它只影响您的工作区树而不是整个系统。

  • 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. Yarn将为每个项目使用一个锁文件,而不是不同的锁文件,这意味着更少的冲突和更轻松的查看。

If you have "workspaces" declared in your package.json; 如果您在package.json中声明了“工作区”; you've opted into workspaces and setup must be correct. 您选择了工作区,并且设置必须正确。 This appears to only make sense if you're using a mono-repo. 仅在使用单仓库时,这才有意义。 As it pertains to this, a mono-repo is one large repo with a "root" package.json where workspaces are declared as well as dev-dependencies. 与此相关的是,mono-repo是一个带有“ root” package.json的大型仓库,其中声明了工作区以及dev-dependencies。 Inside it will be other "packages" (projects with their own package.json, but not their own repo). 它的内部将是其他“包”(具有自己的package.json,但没有自己的存储库的项目)。

When you want to add packages to those "workspaces" ( see https://yarnpkg.com/lang/en/docs/workspaces/ ) or "packages" and you're working from the root folder, you must specify the workspace you're targeting the Yarn action with. 当您想将软件包添加到那些“工作区”(请参阅https://yarnpkg.com/lang/zh/docs/workspaces/ )或“软件包”并且要在根文件夹中工作时,必须指定工作区定位到Yarn操作。

If it's a dev-dependency for the root, this is a non-issue. 如果它是根的dev-dependency,则这不是问题。 Since it's assumed that your "root" project won't have it's own "production" dependencies and they should only be in the workspaces. 因为假定您的“根”项目将没有它自己的“生产”依赖项,所以它们只能在工作区中。 If it's not a dev-dependency and you don't specify the workspace ( https://yarnpkg.com/en/docs/cli/workspace ), then you will see the warning and how to override. 如果不是开发人员依赖性,并且您未指定工作区( https://yarnpkg.com/en/docs/cli/workspace ),那么您将看到警告以及如何覆盖。

Simple Answer简单的答案

You may not have indexed into the correct directory of your app.您可能没有索引到应用程序的正确目录。

For example, you may have a next.js app structured like this:例如,您可能有一个结构如下的next.js应用程序:

myApp
- /app
- /functions

Yarn will throw this warning if you're trying to remove a package while in the myApp directory in your IDE terminal.如果您在 IDE 终端的myApp目录中尝试删除包,Yarn 将抛出此警告。 So, the solution may be as simple as this:因此,解决方案可能就这么简单:

cd /app
yarn remove somePackage

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Yarn Workspaces和Browserify - 子文件夹中的package.json会破坏构建 - Yarn Workspaces and Browserify - package.json in subfolder breaks the build 如何在 Yarn 工作区的一个 package.json 文件中安装软件包? - How to install packages in just one package.json file in Yarn workspaces? npm 和 yarn:package.json 是否比 lockfile 有优先级? - npm and yarn: does package.json have the priority over lockfile? 为什么程序包在我的yarn.lock中而不在我的package.json中? - Why are packages in my yarn.lock but not in my package.json? 如果第二个项目的package.json不在项目的根目录中,如何在package.json中添加github依赖关系 - How to add github dependency in package.json if package.json of second project not in the root of the project 在我更新 package.json 中的依赖项版本并运行 yarn install 或仅运行 yarn 后,yarn.lock 文件未更新 - yarn.lock file is not updating after I update the version of a dependency in package.json and run yarn install or just yarn package.json 中的根目录 - Root directory in package.json Node.js package.json是否需要位于存储库的根目录下? - Does a Node.js package.json need to be at the root of the repository? 出现依赖项错误时,恢复为Package.json的先前版本 - Reverting to Previous Version of Package.json When a Dependency Bug Arises 什么是我的package.json添加输入行,为什么? - What is adding entry lines to my package.json, and why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM