And to clarify, you should run this once by hand to collect the list, don't add the grepping to the actuall installation script or you are back to square one :D
Here's what I use with yarn, install.sh:
#!/usr/bin/env bash
yarn
function run_install() {
local dependency="node_modules/$1"
if [[ -d "$dependency" ]]; then
yarn --cwd "$dependency" run postinstall
fi
}
run_install 'esbuild'
run_install 'other_package'
...