【puppeteer 】 提示Error: Failed to launch the browser process解决办法

时间:2022-08-04浏览次数:2926
通过远程的code-server使用puppeteer实现在nodejs上也能给网页截图, 但是运行官网上的示例报错.如下:const puppeteer = require('puppeteer');(async () => { const browser = await puppeteer.launch() const page = 这是我的一个技术博客网站,主要目地是为了方便自己整理基础知识应用与收集常见技术问题,以便后面出现同样问题可以直接解决;

通过远程的code-server使用puppeteer实现在nodejs上也能给网页截图, 但是运行官网上的示例报错.如下:

const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://bilibili.com/')
  await page.screenshot({ path: 'example.png' })
  await browser.close()
})()

运行后提示error:

Error: Failed to launch the browser process!
/var/app/current/node_modules/puppeteer/.local-chromium/linux-848005./chrome-linux/chrome: error while loading shared libraries: libxkbcommon.so.0: cannot open shared object file: No such file or directory
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/var/app/current/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:193:20)
at Interface.(/var/app/current/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:183:68)
at Interface.emit (events.js:323:22)
at Interface.close (readline.js:409:8)
at Socket.onend (readline.js:187:10)
at Socket.emit (events.js:323:22)
at endReadableNT (_stream_readable.js:1204:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)

于是在github上的puppeteer/issues查询, 网址:

https://github.com/puppeteer/puppeteer/issues/6922

实际上是系统缺少一些包, 而且puppeteer的文档中也写得很详细:在

https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix

#依赖库
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
#字体
yum install 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 -y


之后运行时, 提示另外一个错误:

(node:5267) UnhandledPromiseRejectionWarning: 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/puppeteer/blob/main/docs/troubleshooting.md#setting-up-chrome-linux-sandbox


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


就可以正常运行, 但是官方并不推荐我们这么做就是了, 在截图没截到的下方有其他推荐的做法.

上一条:

yarn的常用命令

Copyright © 2018 网站仅供学习交流使用,无任何商业用途  闽ICP备15015615号  技术支持: 微魔法建站