关于puppeteer项目部署到ubuntu报错记录

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

部署文档

以下例子使用 ubuntu20.04,puppeteer & puppeteer-core 为 23.2.0/23.4.0

时间:2024/09/23

安装依赖

bash 复制代码
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

bash 复制代码
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

bash 复制代码
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地址

javascript 复制代码
const browser = await puppeteer.launch({
        executablePath: process.env.CHROME_PATH,
});

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

bash 复制代码
which google-chrome-stable

到这里,理论上是没问题了,但是可能部分人运行还是有问题

我们可以试试把google-chrome-stable改成安装chromium-browser

然后路径地址填chromium-browser尝试一下问题是否还在

报错2

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

javascript 复制代码
const browser = await puppeteer.launch({
        headless: true
});
bash 复制代码
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
相关推荐
戴为沐13 小时前
Linux内存扩容指南
linux
zylyehuo1 天前
Linux 彻底且安全地删除文件
linux
牛奶1 天前
如何自己写一个浏览器插件?
前端·chrome·浏览器
用户805533698032 天前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
用户034095297912 天前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux
乘云数字DATABUFF2 天前
5分钟部署开源APM Databuff:OpenTelemetry全链路追踪入门实战
运维·后端
Web3探索者3 天前
可视化服务器管理和传统命令行区别是什么?新手教程:Linux 运维到底该用图形界面还是 SSH 命令行?
linux·ssh
zylyehuo3 天前
Linux系统中网线与USB网络共享冲突
linux
荣--3 天前
一键部署不是为了省时间 —— 它是把"买来的 PaaS"变成"自己的平台"的拐点
运维·zabbix·工程化·一键部署·平台化·边界设计
江华森4 天前
动手实战学 Docker — 从零到集群编排完全指南
运维