【问题标题】:nohoist with workspaces still hoisting工作空间仍在吊装的 nohoist
【发布时间】:2019-11-02 16:16:05
【问题描述】:

在我的 Monorepo 中,我有一个包,我希望在它的 node_modules 中包含所有依赖项。

但无论我做什么,它的 node_modules 都是空的。

因此,就我的问题而言,我能够通过以下设置重现该问题

/
 package.json
 lerna.json
 node_modules
 packages/
          A/
            node_modules
            package.json
            index.ts
          B/
            node_modules
            package.json
            index.ts

我为此创建了一个repo

主包.json

{
  "name": "A-B-test",
  "private": true,
  "workspaces": {
    "packages": ["packages/*"],
    "nohoist": [ "**/B" ]
  },
  ...
  "devDependencies": {
    "lerna": "^3.13.4"
  }
}

B/package.json 看起来像

{
  "name": "@scaljeri/B",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "angular": "^1.7.8"
  },
  "devDependencies": {
    "browserify": "^16.2.3",
    "typescript": "^3.5.2"
  }
}

现在当我在项目的根目录中运行yarn 时,所有依赖项都安装在根目录node_modules 中。

纱线版本​​:1.16.0 节点:12.4.0

任何建议可能是什么问题?

【问题讨论】:

标签: yarnpkg monorepo yarn-workspaces


【解决方案1】:

根据我的经验,有必要以下列方式双重指定每个包:

{
  "nohoist": [
    "**/B",
    "**/B/**",
    "**/C",
    "**/C/**"
  ]
}

此外,我发现在修改nohoist 设置后,有必要删除所有现有的node_modules 文件夹。因此,如果您的项目位于 packages 中,并且您位于项目目录中,则可以像这样删除所有这些 node_modules 文件夹:

# delete node_modules on linux
rm -rf ./packages/*/node_modules
rm -rf ./node_modules

# install again
yarn install --check-files

【讨论】:

  • 我还必须删除我的 yarn.lock 文件才能让 Yarn 不提升包裹。
  • 你也可以通过lerna clean清理包的所有节点模块,这样你也可以选择你想要真正清理的那些
【解决方案2】:

将 nohoist 应用到子包的 package.json 为我做了。

  "workspaces": {
    "nohoist": [
      "*react-native*",
      "*react-native*/**"
    ]
  },

【讨论】:

  • 这是唯一适用于 yarn 1.22 的解决方案。谢谢!
  • 这对我不起作用(1.22.4)。 Yarn 报错因为子包不是private
【解决方案3】:

如果您想从提升中排除包 B 节点模块,您可以在 package.json 中执行此操作:

  "workspaces": {
    "nohoist": [
      "**/B",
      "**/B/**"
    ]
  },

之后删除 node_modulesyarn.lock 并再次运行 yarn install 现在您应该会看到 packages/B/node_modules 没有被提升到项目的顶层

【讨论】:

    【解决方案4】:

    我尝试了几个选项,但最终对我有用的唯一一个是在子包中指定 nohoist 选项。无需在根级别指定任何内容。
    所以我在B 包中的package.json 是:

    {
    ...
    
      "workspaces": {
        "nohoist": ["**"]
      },
    
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-30
      • 2021-12-18
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多