nohoist with workspaces still hoisting
Asked Answered
O

5

30

Inside my Monorepo I have one packages in which I want all the dependencies inside its node_modules.

But whatever I do, it's node_modules remains empty.

So, for the purpose of my question I was able to reproduce the issue with the following setup

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

I've created a repo for this!

Main package.json

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

B/package.json looks like

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

Now when I run yarn in the root of the project, all dependencies are installed in the root node_modules.

yarn version: 1.16.0 node: 12.4.0

Any suggestions what might be the problem?

Ortensia answered 19/6, 2019 at 21:9 Comment(1)
For anyone else who is on Yarn version 2 or higher: "workspaces.nohoist" has been replaced with "installConfig.hoistingLimits". yarnpkg.com/configuration/manifest#installConfigClabo
F
41

In my experience, it has been necessary to doubly specify each package in the following manner:

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

In addition, I have found it necessary to delete all existing node_modules folders after modifying the nohoist settings. So, if your projects are in packages, and you are in your project directory, you can delete all of those node_modules folders like this:

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

# install again
yarn install --check-files
Foot answered 13/12, 2019 at 19:52 Comment(2)
I also had to delete my yarn.lock file to get Yarn to not hoist a package.Libeler
you can also clean up all node modules of packages through lerna clean which will let you also select the ones you want to really clean upSymptom
S
24

I tried a few options but the only one that finally worked for me was specifying the nohoist option in the child package. No need to specify anything at the root level.
So my package.json in B package is:

{
...

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

...
}
Sizar answered 29/3, 2021 at 20:59 Comment(2)
this one should be marked as correct answer. None of above worked for me.Ataxia
Also package should have private: "true" in order for this to workSverige
S
5

Applying the nohoist to the child package's package.json did it for me.

  "workspaces": {
    "nohoist": [
      "*react-native*",
      "*react-native*/**"
    ]
  },
Stalinsk answered 12/1, 2020 at 6:19 Comment(2)
This is the only solution that worked with yarn 1.22. Thanks!Bowra
This didn't work for me (1.22.4). Yarn showed an error because the child package was not privateRevalue
R
0

If you want to exclude package B node modules from hoisting you can do that in you package.json:

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

after that remove node_modules and yarn.lock and run yarn install again now you should see packages/B/node_modules not being hoisted to the top level of the project

Rosenbaum answered 5/2, 2021 at 0:48 Comment(0)
B
0

The only thing that worked for me on yarn 3.6.4 was adding nmHoistingLimits: workspaces to my root .yarnrc.yml file

Belaud answered 12/10, 2023 at 21:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.