nohoist 工作区仍在吊装

nohoist with workspaces still hoisting

在我的 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

主要package.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

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

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

{
  "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

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

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

如果你想从提升中排除包 B 节点模块,你可以在 package.json:

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

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

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

{
...

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

...
}