【问题标题】:Yarn nohoist without using workspaces不使用工作区的纱线 nohoist
【发布时间】:2021-12-18 17:30:12
【问题描述】:

我的一个项目突然无法在 Windows 笔记本电脑上编译,而在 Mac 上运行完全相同的代码。我已经阅读了关于提升和添加 nohoist 的信息,这似乎解决了 Apollo 客户端的问题。

"workspaces": {
    "packages": [
      "packages/*"
    ],
    "nohoist": [
      "**/tslib",
      "**/tslib/**"
    ]
}

现在,我不使用工作区,但由于我在 package.json 中使用上面的代码,Yarn 在添加或删除包时要求-W 参数说:

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

在我看来,这不是最好的方法。我该怎么办?

【问题讨论】:

    标签: javascript react-native apollo yarnpkg hoisting


    【解决方案1】:

    通过在package.json 中添加工作区配置,您已启用工作区的使用。这就是您收到有关将依赖项添加到工作区根目录的警告的原因。 nohoist 是仅工作区选项;创建它是为了使与 yarn 工作区提升方案不兼容的 3rd 方库仍可在工作区下使用。见https://classic.yarnpkg.com/blog/2018/02/15/nohoist/

    “nohoist”使工作空间能够使用尚未与其提升方案兼容的第 3 方库。这个想法是禁止选定的模块被提升到项目根目录。它们被放置在实际(子)项目中,就像在独立的非工作区项目中一样。

    如果您不想使用工作区,则需要从您的package.json 中删除工作区配置并以另一种方式修复您的编译问题。

    【讨论】:

    • 我明白,但我觉得奇怪的是,虽然我不使用工作区,但这个选项解决了我的问题,正如论坛上所建议的那样
    【解决方案2】:

    实际上,使用以下配置排除某些需要在工作区包中具有不同版本的包是正确的方法:

    "workspaces": {
      "nohoist": [
        "**/[package-name]",
        "**/[package-name]/**"
      ]
    }
    

    这将有助于成功构建。

    【讨论】:

    • 感谢您的回答。我知道它有效,但我仍然不明白为什么我现在必须在yarn 中使用-w 标志
    • @Z0q,因为你有workspaces,所以你应该使用-w 标志来为你的纱线配置工作空间。
    猜你喜欢
    • 2020-09-30
    • 2018-07-18
    • 2019-07-13
    • 2020-08-21
    • 2021-06-03
    • 2018-12-10
    • 2022-08-02
    • 2021-08-08
    • 1970-01-01
    相关资源
    最近更新 更多