From inside a yarn workspace subpackage, run a root-level script
Asked Answered
U

3

11

I'm wondering: if your terminal's current working directory is inside a yarn workspace, is there a way to run a yarn script that's defined at the project root without changing the current directory to be outside of a workspace?

For instance, you can run a command for a particular workspace by running yarn workspace workspace-name script-name but is it possible to use that yarn workspace command to target not a subpackage, but the root package itself?

Unger answered 17/3, 2021 at 9:13 Comment(0)
P
10

I couldn't find a way to do it with yarn workspace, but you can do it by specifying the current working directory (cwd) when running the root command. Assuming you're running your command from ~/packages/subpackage, you'll need to go back two times with ../..:

yarn --cwd="../.." my-root-script
Pleading answered 6/12, 2021 at 7:51 Comment(1)
More recent versions of Yarn also include PROJECT_CWD and INIT_CWD as described in the docs.Distemper
D
3

Scripts that contain a : in their name can be run from anywhere!

For example, your root script called "root:something" can be called from within any workspace by running yarn root:something.

Note that this even works if the : script is not a root script, but a workspace script. See yarn docs.

Deedradeeds answered 20/3, 2022 at 6:12 Comment(2)
Superb. This works.Douglasdouglashome
This is specific for yarn 2Alibi
A
2

I just want to add, that with yarn berry, you can use the -T option on the run command.

yarn run -T my-top-level-script
Adp answered 12/3, 2023 at 1:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.