npm安装prerender-spa-plugin时依赖包puppeteer报错

2,680 阅读2分钟

npm安装依赖prerender-spa-plugin时报错Failed to download Chromium r686378! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.详细信息如下:

[91mERROR: Failed to download Chromium r686378! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.
{ Error: read ECONNRESET
    at _errnoException (util.js:992:11)
    at TLSWrap.onread (net.js:618:25)
  -- ASYNC --
    at BrowserFetcher.<anonymous> (/nerv.app/node_modules/@prerenderer/renderer-puppeteer/node_modules/puppeteer/lib/helper.js:111:15)
    at Object.<anonymous> (/nerv.app/node_modules/@prerenderer/renderer-puppeteer/node_modules/puppeteer/install.js:64:16)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3 code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

由于prerender-spa-plugin依赖puppeteer附带的chromium,国内下载经常会很慢或失败,所以建议执行以下命令修改puppeteer镜像源:

npm config set puppeteer_download_host=https://storage.googleapis.com.cnpmjs.org

修改完镜像源可以正常安装,但是执行npm run build时又报错Error: Failed to launch chrome!详细信息如下:

-  Building for pre...
Error: Failed to launch chrome!
/nerv.app/node_modules/puppeteer/.local-chromium/linux-686378/chrome-linux/chrome: error while loading shared libraries: libX11.so.6: cannot open shared object file: No such file or directory

TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

    at onClose (/nerv.app/node_modules/puppeteer/lib/Launcher.js:348:14)
    at Interface.helper.addEventListener (/nerv.app/node_modules/puppeteer/lib/Launcher.js:337:50)
    at emitNone (events.js:111:20)
    at Interface.emit (events.js:208:7)
    at Interface.close (readline.js:368:8)
    at Socket.onend (readline.js:147:10)
    at emitNone (events.js:111:20)
    at Socket.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
[Prerenderer - PuppeteerRenderer] Unable to start Puppeteer
(node:16) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'close' of null
    at PuppeteerRenderer.destroy (/nerv.app/node_modules/@prerenderer/renderer-puppeteer/es6/renderer.js:140:21)
    at Prerenderer.destroy (/nerv.app/node_modules/@prerenderer/prerenderer/es6/index.js:87:20)
    at PrerendererInstance.initialize.then.then.then.then.then.then.then.then.catch.err (/nerv.app/node_modules/prerender-spa-plugin/es6/index.js:144:29)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:16) 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:16) [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.
Build was aborted

网上查找发现,项目部署Linux的服务器并不支持chromium可视化界面,所以需要安装一些依赖来支持,用到的依赖如下:

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

注:由于项目的服务器是在国内,下载会非常的慢,建议把镜像源换为国内阿里sudo vim /etc/apt/sources.list,添加以下内容:

deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb http://mirrors.aliyun.com/debian/ jessie-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie-updates main non-free contrib
deb http://mirrors.aliyun.com/debian-security/ jessie/updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian-security/ jessie/updates main non-free contribav

安装以上依赖后,执行成功。

总结:本项目是vuejs项目,在本地使用prerender-spa-plugin时并没有出现以上情况,错误是在docker创建镜像中出现,主要原因是docker基础镜像为debian gnu/linux系统,导致的chromium运行失败,apt-get安装以上依赖模块后就可以正常运行了。(踩到的几处坑是记住要切换到国内镜像😂)