I am trying to use the npm package critical (critical - npm).
It uses headless chromium through puppeteer. It used to work in node 14.x, but with node 22.x the following error raises:
Error: Failed to launch the browser process!
/vercel/path0/node_modules/puppeteer/.local-chromium/linux-722234/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md
The following cli script is the one raising the error:
The most reliable solution would probably be to generate the critical CSS either locally or in a GitHub Action, where you have more control over the environment and can install the required system dependencies. This would avoid the limitations of the serverless environment while still allowing you to use Node.js 22 and the critical package. For example:
Run critical CSS generation locally:
# Local script
npm run critical-css
git add assets/css/critical.css
git commit -m "Update critical CSS"