6

One of my projects suddenly failed to compile on a Windows laptop, where the exact same code was working on a Mac. I've read about hoisting and adding nohoist, which seemed to fix the problem for Apollo client.

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

Now, I don't use workspaces, but since I am using the code above in package.json, Yarn asks for the -W parameter when adding or removing packages saying:

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

It doesn't seem to me like this is the best way to go. What should I do?

Z0q
  • 1,689
  • 3
  • 28
  • 57

2 Answers2

0

By adding the workspaces config in your package.json you have enabled the use of workspaces. This is why you're getting the warning about adding the dependency to the workspace root. nohoist is a workspace only option; it was created to enable 3rd party libraries that are not compatible with the yarn workspace hoisting scheme to still be used under workspaces. See https://classic.yarnpkg.com/blog/2018/02/15/nohoist/

“nohoist” enables workspaces to consume 3rd-party libraries not yet compatible with its hoisting scheme. The idea is to disable the selected modules from being hoisted to the project root. They were placed in the actual (child) project instead, just like in a standalone, non-workspaces, project.

If you don't want to use workspaces, you need to remove the workspace configuration from your package.json and fix your compilation issue another way.

Thijs
  • 587
  • 5
  • 16
  • I understand, but I find it strange that while I don't use workspaces, this option solves my issue, as suggested on a forum – Z0q Nov 15 '21 at 12:46
0

Actually, it is the proper way to exclude some packages which it is needed to have different versions of them in your workspaces packages by using the below config:

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

This will help to have successful build.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
  • Thank you for your answer. I understand that it works, but I still don't understand why I have to use the `-w` flag in `yarn` now – Z0q Nov 24 '21 at 15:17
  • @Z0q, because you have `workspaces`, so you should use `-w` flag for having configuration of workspaces for you yarn. – AmerllicA Nov 24 '21 at 22:07