centos8安装puppeteer

发布时间 2023-07-16 14:52:35作者: 丁少华

背景

我计划使用puppeteer爬点html数据,结果windows11上没问题 但在我的服务器centos8上确报错。

[root@104 auto-task]# npm run start
> auto-task@1.0.0 start
> node src/main.js
启动成功:http://localhost:3000
  Error: Failed to launch the browser process!
  /root/.cache/puppeteer/chrome/linux-114.0.5735.133/chrome-linux64/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
  
  TROUBLESHOOTING: https://pptr.dev/troubleshooting
      at Interface.onClose (file:///home/auto-task/node_modules/@puppeteer/browsers/lib/esm/launch.js:250:24)
      at Interface.emit (node:events:525:35)
      at Interface.close (node:internal/readline/interface:533:10)
      at Socket.onend (node:internal/readline/interface:259:10)
      at Socket.emit (node:events:525:35)
      at endReadableNT (node:internal/streams/readable:1359:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

原因分析

网上查找资料得知,本来就是不可能能一凡风顺,官方给出了要想成功使用的必备依赖包

安装必备依赖

yum install -y 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 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc

可能出现的问题

无法安装依赖ipa-gothic-fonts
但是我执行以上的安装这些依赖包命令,结果却报错,导致安装依赖失败。
如果你没有下边这个报错,请忽略此环节。

Failed to set locale, defaulting to C
Last metadata expiration check: 0:39:16 ago on Sun Jul 16 01:42:31 2023.
No match for argument: ipa-gothic-fonts
Error: Unable to find a match

还好英语我的英语略懂,根据提示可得知 依赖包 ipa-gothic-fonts找不到。 推测作者可能撤包,也可能是源的问题。
办法简单,通过查找资料获取ipa-gothic-fonts安装包,手动安装即可。

curl -O https://pkgs.dyn.su/el8/base/x86_64/ipa-gothic-fonts-003.03-15.el8.noarch.rpm
yum install -y ipa-gothic-fonts-003.03-15.el8.noarch.rpm

然后再通过命令安装剩余的依赖即可

yum install -y 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 xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc

升级nss依赖

根据官方要求 升级nss依赖

yum update nss -y

可能出现的问题

执行以上我这里报错 提示我服务器上压根就没有nss这个包,

[root@104 ~]# yum update nss -y
Last metadata expiration check: 0:17:42 ago on Sun 16 Jul 2023 12:49:13 AM EDT.
Package nss available, but not installed.
No match for argument: nss
Error: No packages marked for upgrade.

那更好办了,直接全新安装即可

yum install nss -y

还是不能运行?

是的,按照官方要求,安装了上诉的这些必备依赖项之后,还是提示报错。这是怎么回事?

[root@104 auto-task]# npm run start
> auto-task@1.0.0 start
> nodesrc/main.js
启动成功:http://localhost:3000
  Error: Failed to launch the browser process!
  /root/.cache/puppeteer/chrome/linux-114.0.5735.133/chrome-linux64/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
  
  TROUBLESHOOTING: https://pptr.dev/troubleshooting
      at Interface.onClose (file:///home/auto-task/node_modules/@puppeteer/browsers/lib/esm/launch.js:250:24)
      at Interface.emit (node:events:525:35)
      at Interface.close (node:internal/readline/interface:533:10)
      at Socket.onend (node:internal/readline/interface:259:10)
      at Socket.emit (node:events:525:35)
      at endReadableNT (node:internal/streams/readable:1359:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

根据分析 应该(chrome运行时的)依赖仍然缺少。我们去验证下

cd /root/.cache/puppeteer/chrome/linux-114.0.5735.133/chrome-linux64
ldd chrome | grep not

下边可以看到缺少哪些依赖 导致chrome启动失败

[root@104 chrome-linux64]# ldd chrome | grep not
        libdrm.so.2 => not found
        libgbm.so.1 => not found

那我们安装这些缺少的依赖即可

yum install libdrm libgbm libxshmfence -y

最后再次执行上边的命令看一次是否缺少依赖,如果不出意外,全部依赖安装成功!

结语

至此,再试试你的程序 是不是可以正常运行puppeteer了!