How in Yarn 2 do I share common dependencies in workspaces?

3.3k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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"
  }
}
0
On

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