关于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
相关推荐
我命由我123451 分钟前
IDEA - IDEA 快速回到页面首尾、页面快速滑动、快速定位到指定行
java·运维·ide·后端·java-ee·intellij-idea·intellij idea
AI小小怪4 小时前
在Linux服务器上安装CVAT (Docker 28.5.1)
运维·docker·容器·数据标注·cvat
TeleostNaCl5 小时前
解决 Chrome 无法访问网页但无痕模式下可以访问该网页 的问题
前端·网络·chrome·windows·经验分享
KYGALYX7 小时前
在Linux中备份msyql数据库和表的详细操作
linux·运维·数据库
余—笙8 小时前
Linux(docker)安装搭建CuteHttpFileServer/chfs文件共享服务器
linux·服务器·docker
lang201509288 小时前
Linux高效备份:tar与gzip完全指南
linux·运维·服务器
IDOlaoluo8 小时前
OceanBase all-in-one 4.2.0.0 安装教程(CentOS 7/EL7 一键部署详细步骤)
linux·centos·oceanbase
wanhengidc9 小时前
云手机的基本原理
运维·服务器·游戏·智能手机·云计算
篙芷9 小时前
两台服务器 NFS 共享目录实战
运维·服务器
catoop9 小时前
在 WSL 的 Ubuntu 中安装和配置 SSH 服务
linux·ubuntu·ssh