likes
comments
collection
share

centos7 puppeteer Error: Failed to launch the brow

作者站长头像
站长
· 阅读数 23

错误

运行后提示Error: Failed to launch the browser process!......

排查

github puppeteer/issues:

https://github.com/puppeteer/...

原因是系统缺少一些包,puppeteer的文档中也有说明:

https://github.com/puppeteer/...

解决

按照文档安装依赖包:

yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 -y

重启node服务,依旧报错:

Error: Failed to launch the browser process!
[0308/163337.932654:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

文档中也有提到:https://github.com/puppeteer/...

原因是在root权限下打开了Chromium,可以换角色运行node服务,另一种解决方式是,开启Chromium时传入参数:

const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] })

puppeteer不建议这么做,但我这里是测试,无所谓了,生产环境切勿这么做。

重启node服务,截图成功但中文乱码,原因是,centos7中缺失中文字体的原因。

解决缺失字体

下载安装字体即可:

  1. 安装字体管理工具安装fontconfig来管理字体库
yum -y install fontconfig

安装完fontconfig时,在/usr/shared目录就可以看到fonts和fontconfig目录了(之前是没有的)

查看所有字体:fc-list查看中文字体:fc-list :lang=zh

  1. 创建字体目录并并修改权限

创建字体目录:

mkdir /usr/share/fonts/chinese

修改字体文件的权限,使root用户以外的用户也可以使用:

chmod -R 755 /usr/share/fonts/chinese
  1. COPY字体到/usr/share/fonts/chinese

在 windows 的 C:\Windows\Fonts目录下找到需要的字体, copy 到 /usr/share/fonts目录下,这里添加雅黑和新宋体:msyh.ttc,simsun.ttc

  1. 建立字体索引信息,更新字体缓存
cd /usr/share/fonts/chinese
mkfontscale //如果提示 mkfontscale: command not found,需自行安装 # yum install mkfontscale
mkfontdir
fc-cache -fv //如果提示 fc-cache: command not found,则需要安装# yum install fontconfig