How in Yarn 2 do I share common dependencies in workspaces?
Asked Answered
B

2

8

Mostly the same question as this, but for yarn 2. I put my shared dependencies at the top of the hierarchy. I believe I'm not using PnP currently.

.yarnrc.yaml

nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-2.2.2.cjs

at the very top level I have typescript installed (but I presume this could be any module with a binary) down in one of my "workspaces" I want to call tsc, however it's command not found: tsc I've also noticed some warnings like. graph@workspace:app-lib/graph/packages/app doesn't provide jest@>=24 <25 requested by ts-jest@npm:24.3.0 which is provided in the parent of app.

Babette answered 3/9, 2020 at 20:40 Comment(0)
B
7

https://yarnpkg.com/advanced/qa#how-to-share-scripts-between-workspaces

Little-known Yarn feature: any script with a colon in its name (build:foo) can be called from any workspace. Another little-known feature: $INIT_CWD will always point to the directory running the script. Put together, you can write scripts that can be reused this way:

{
  "dependencies": {
    "typescript": "^3.8.0"
  },
  "scripts": {
    "g:tsc": "cd $INIT_CWD && tsc"
  }
}

Then, from any workspace that contains its own tsconfig.json, you'll be able to call TypeScript:

{
  "scripts": {
    "build": "yarn g:tsc"
  }
}
Babette answered 4/9, 2020 at 17:34 Comment(0)
C
4

You can use -T,--top-level flag with yarn run. It will look for root level dependencies.

{
  "scripts": {
    "tsc": "yarn run -T tsc"
  }
}

https://yarnpkg.com/cli/run

Contend answered 6/8, 2021 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.