关于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
相关推荐
12点一刻17 分钟前
搭建自动化工作流:探寻解放双手的有效方案(1)
运维·人工智能·自动化·deepseek
Hi2024021717 分钟前
自动化Trae Apollo参数解释的批量获取
运维·自动化·trae
sky丶Mamba1 小时前
CMD,PowerShell、Linux/MAC设置环境变量
linux·运维·macos
运维开发王义杰1 小时前
打破技术债困境:从“保持现状”到成为变革的推动者
运维·职场和发展
工藤新一¹1 小时前
Linux 开发工具
linux·运维·服务器
W111115_1 小时前
网络综合实验
linux·运维·服务器·网络
cui_win1 小时前
【网络】Linux 内核优化实战 - net.ipv4.tcp_sack
linux·网络·tcp/ip
x县豆瓣酱1 小时前
ubuntu server远程连接
linux·运维·ubuntu
0wioiw01 小时前
Ubuntu基础(Python虚拟环境和Vue)
linux·python·ubuntu
史不了1 小时前
无 sudo 运行:让你的程序在 Ubuntu 低端口监听
linux·运维·ubuntu