7

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.

RobC
  • 22,977
  • 20
  • 73
  • 80
xenoterracide
  • 16,274
  • 24
  • 118
  • 243

2 Answers2

6

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"
  }
}
xenoterracide
  • 16,274
  • 24
  • 118
  • 243
1

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

Archinowsk
  • 41
  • 5