关于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
相关推荐
未济5 天前
linux 配置环境变量
linux
傲世仙尊5 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫5 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
_艾伦 耶格尔.5 天前
进程间通信
linux
AI职业加油站5 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
Liuqy-055 天前
Linux IO编程——静态库、动态库
linux
此冬歌咏5 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
彧azz5 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅5 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK5 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid