木偶师未在无头模式下运行:错误模式

人气:1,273 发布:2022-10-16 标签: node.js google-chrome puppeteer headless

问题描述

我正尝试在无头:错误模式下启动木偶操纵者。它在我的本地计算机上工作,但当我将其推送到服务器并尝试启动它时,我收到以下错误:

4|scraperP | You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
4|scraperP | Error: Failed to launch chrome!
4|scraperP | [0620/073557.986542:ERROR:nacl_helper_linux.cc(310)] NaCl helper process running without a sandbox!
4|scraperP | Most likely you need to configure your SUID sandbox correctly
4|scraperP | TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
4|scraperP |     at onClose (/home/pjotr/scrapermmcreation/node_modules/puppeteer/lib/Launcher.js:285:14)
4|scraperP |     at Interface.helper.addEventListener (/home/pjotr/scrapermmcreation/node_modules/puppeteer/lib/Launcher.js:274:50)
4|scraperP |     at Interface.emit (events.js:165:20)
4|scraperP |     at Interface.close (readline.js:381:8)
4|scraperP |     at Socket.onend (readline.js:154:10)
4|scraperP |     at Socket.emit (events.js:165:20)
4|scraperP |     at endReadableNT (_stream_readable.js:1101:12)
4|scraperP |     at process._tickCallback (internal/process/next_tick.js:152:19)

当我在Headless:True模式下启动时,我没有收到错误消息。你知道怎么解决这个问题吗?这就是我如何启动木偶操纵者:

var browser = await puppeteer.launch({
   args: [
   '--ignore-certificate-errors',
   '--no-sandbox',
   '--disable-setuid-sandbox',
   '--window-size=1920,1080',
   "--disable-accelerated-2d-canvas",
   "--disable-gpu"],
   ignoreHTTPSErrors: true,
   headless: false,
 });

推荐答案

1.您必须安装一些lib包。

gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 
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 
ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget 
xvfb x11vnc x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps

2.然后使用脚本启动xvfb

示例:

xvfb-run --server-args="-screen 0 1024x768x24" npm start

如果您使用Docker,请遵循此docker文件

FROM node:8
RUN apt-get update && 
apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 
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 
ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget 
xvfb x11vnc x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 8081
CMD xvfb-run --server-args="-screen 0 1024x768x24" npm start
以下是使用xvfb的木偶操纵者的示例 https://github.com/nsourov/Puppeteer-with-xvfb

415