我的项目是nest+puppeteer的,但这里只记录puppeteer的问题,当然,我在windows上进行开发的时候是不出现任何问题的

部署文档

以下例子使用 ubuntu20.04,puppeteer & puppeteer-core 为 23.2.0/23.4.0
时间:2024/09/23

安装依赖

apt-get install ca-certificates fonts-liberation libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils -y

正常情况下安装完依赖后,就可以直接部署上了,部分系统可能有例外,需要额外进行以下操作,因此有了这个文档

额外配置

如果你出现了以下报错,代表可能没有自动安装chrome,可以尝试

node node_modules/puppeteer/index.mjs
or
npx puppeteer browsers install chrome@xxx

报错1

Could not find Chrome (ver. 128.0.6613.84). 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.

如果还不行,尝试手动安装:google-chrome-stable

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add - 
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' 
sudo apt update 
sudo apt install google-chrome-stable 

然后我们手动给puppeteer添加上chrome地址

const browser = await puppeteer.launch({
        executablePath: process.env.CHROME_PATH,
});

上面的:process.env.CHROME_PATH 可以通过以下路径获取,也就是上面安装的google-chrome-stable路径

which google-chrome-stable

到这里,理论上是没问题了,但是可能部分人运行还是有问题
我们可以试试把google-chrome-stable改成安装chromium-browser
然后路径地址填chromium-browser尝试一下问题是否还在

报错2

出现以下问题,你先看看你部署的是不是拥有图形化界面的linux,如果不是,填上:headless: true,我因为是服务器,不存在图形化界面所以仅仅是设置了这个就解决了报错问题,如果你是其他原因出现这个问题,可以尝试看看这个issue:https://github.com/puppeteer/puppeteer/issues/8148

const browser = await puppeteer.launch({
        headless: true
});
Failed to launch the browser process!
[8017:8017:0923/172632.836145:ERROR:ozone_platform_x11.cc(244)] Missing X server or $DISPLAY
[8017:8017:0923/172632.836187:ERROR:env.cc(258)] The platform failed to initialize.  Exiting.

TROUBLESHOOTING: https://pptr.dev/troubleshooting

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部