背景
在NestJS中使用puppeteer来做无头浏览器请求, 本地调试成功后打包上服务器用PM2来部署NestJS 其中执行到调用puppeteer时会报错
Error: Could not find Chrome (ver. 121.0.6167.85). This can occur if either
1. you did not perform an installation before running the script (e.g. `npx puppeteer browsers install chrome`) or
2. your cache path is incorrectly configured (which is: /home/www/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
原因在与puppeteer无法找到对应版本的Chromium
解决办法
到官网找到下载安装对应版本Chromium developer.chrome.com/blog/chrome… 在服务器上用命令行即可
npx @puppeteer/browsers install chrome@stable
下载完成后写入路径即可
executablePath =
'/www/chrome/linux-121.0.6167.85/chrome-linux64/chrome';
console.log('启动浏览器', url);
const browser = await puppeteer.launch({
headless: this.headless,
executablePath: this.executablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});