完整的报错信息如下:
{ Error: socket hang up at createHangUpError (_http_client.js:330:15) at Socket.socketOnEnd (_http_client.js:423:23) at Socket.emit (events.js:164:20) at endReadableNT (_stream_readable.js:1062:12) at process._tickCallback (internal/process/next_tick.js:152:19) code: 'ECONNRESET' } (node:29975) UnhandledPromiseRejectionWarning: Error: socket hang up at createHangUpError (_http_client.js:330:15) at Socket.socketOnEnd (_http_client.js:423:23) at Socket.emit (events.js:164:20) at endReadableNT (_stream_readable.js:1062:12) at process._tickCallback (internal/process/next_tick.js:152:19) (node:29975) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:29975) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
通过Google在https://github.com/GoogleChrome/puppeteer/issues/391#issuecomment-325420271找到可用的解决方法:
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 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
完整安装以上依赖就可以解决错误。
另外,在linux root账号下启动的puppeteer必须使用无沙箱模式,即:
const browser = await puppeteer.launch({ args: ['--no-sandbox']})
否则会报错。