Recursively Run Scripts in All pnpm Workspace Projects

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 3 weeks ago

While its convenient to be able to use filter commands to run scripts without having to cd into package/app folders, it's still a tedious task if you need to run commands in several projects at once.

It's actually very easy to recursively run a script in all projects with pnpm using the -r flag. For example, pnpm run -r build will go through each project in the workspace and attempt to run the build command.

Instructor: [0:00] We have this Remix app in our apps folder and that shared UI React library node packages folder within our pnpm workspace. We can obviously target the single packages directly using the pnpm filter, let's say shared UI. Let's say we want to run the build here, so we run shared UI build, and it is with them build our shared UI package. [0:24] Sometimes you might want to run commands across all the applications and target, let's say, all the build commands and build all the apps and libraries there. To do that, we can use the -R commands, which recursively runs a given command.

[0:39] We can run pnpm, run -R, build. What this will do is it figures out which packages need to be run. In this case, it's packages shared UI and then apps, my Remix app, we'll run build for both of them. This obviously also works for every other command that you want to run from some package.json script.