
I have several Node.js scripts that I run regularly using pnpm.
They’re all similar, syncing remote data into my own database.
For example, I have this system to handle Amazon products and orders using their Amazon SP-API. To simplify, I need to sync these data daily: products, orders, and shipments.
So I created 3 scripts and define them in package.json like this:
{
"scripts": {
"sync:products": "node scripts/sync-products.ts",
"sync:orders": "node scripts/sync-orders.ts",
"sync:shipments": "node scripts/sync-shipments.ts"
}
}So the simplest way is to run them one by one, chaining each pnpm run call together with &&:
pnpm run sync:products && pnpm run sync:orders && pnpm run sync:shipmentsIt works, but it’s ugly. What if I add a new script in the future?
The longer the list gets, the harder it is to read and maintain.
What I want is to run all the sync:* scripts with a single run call such as pnpm run sync-all, without worrying about appending new scripts in the future.
As it turns out, pnpm already supports running multiple scripts at once.
Here’s how.
Run Multiple pnpm Scripts by Name Pattern
As I told you, pnpm already supports running multiple scripts with a single call.
All you have to do is pass a regular expression instead of a script name.
So you can do that from the terminal with something like this.
pnpm run "/^sync:.*/"pnpm will match every script whose name starts with sync:.
In my case, it will match sync:products, sync:orders, and sync:shipments.
Then pnpm will run all matched script names.
The selector must be a regular expression literal, wrapped in slashes, and quoted so your shell does not mangle it.
Matching is not anchored by default, so a pattern like /sync:/ also matches presync:data.
Anchor the pattern with ^ and $ when you only want an exact prefix, like /^sync:.*$/.
If you’re confused with the regular expression, you can always ask your favorite AI agent to generate it.
Sequential or Parallel Run?
By default, since pnpm v12.2.0, the matched scripts run concurrently (parallel), with each script’s output prefixed by its name.
That’s great when you want to watch multiple builds or dev servers at the same time.
But my sync scripts all write into the same database, so running them at the same time risks conflicts and races.
For cases like this, pnpm provides a --sequential flag (added in v11.14.0).
Since my data isn’t that big, syncing them sequentially won’t take a long time.
You can run it sequentially with this example command.
pnpm run --sequential "/^sync:.*/"With --sequential, pnpm pins the concurrency down to 1 and runs the matched scripts one at a time.
The scripts always run in lexicographical order (sorted by string name), so the execution order is deterministic.
In my case, it’ll run the script with this order: sync:orders, sync:products, then sync:shipments.
The Final package.json
Honestly, regex is not my strong suit.
So, instead of typing the regular expression manually in Terminal, I prefer to add a new sync-all script to my package.json.
{
"scripts": {
"sync:products": "node scripts/sync-products.ts",
"sync:orders": "node scripts/sync-orders.ts",
"sync:shipments": "node scripts/sync-shipments.ts",
"sync-all": "pnpm run --sequential \"/^sync:.*/\"",
}
}Then run the whole thing with a single command:
pnpm sync-allpnpm sync-all is just shorthand for pnpm run sync-all, so it runs exactly the sync-all script we just defined.
Final Thoughts
Thanks for reading this short article. I hope it helps you simplify your pnpm scripts as much as it helped me.
As usual, if you have any questions or a better method, leave a comment below. Thanks for reading, and see you next time!