Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 868
xenoterracide Avatar asked Sep 03 '20 20:09

xenoterracide


1 Answers

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"
  }
}
like image 137
xenoterracide Avatar answered Sep 30 '22 17:09

xenoterracide