Yarn nohoist without using workspaces
Asked Answered
I

2

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?

Injury answered 4/11, 2021 at 13:31 Comment(0)
P
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.

Present answered 12/11, 2021 at 16:45 Comment(1)
I understand, but I find it strange that while I don't use workspaces, this option solves my issue, as suggested on a forumInjury
L
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.

Lilongwe answered 24/11, 2021 at 11:27 Comment(2)
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 nowInjury
@Z0q, because you have workspaces, so you should use -w flag for having configuration of workspaces for you yarn.Lilongwe

© 2022 - 2024 — McMap. All rights reserved.