错误信息: permission denied, mkdir '/root/.nvm/versions/node/v9.11.1/lib/node_modules/
今天想在服务器上跑下自己自动签到的程序,需要安装puppeteer ,结果出错。
找了下资料,发现还有不少人出这样的问题,参考 https://github.com/npm/npm/issues/17268
最终使用以下命令,还未安装成功,不过没有报错。
npm install puppeteer -g --unsafe-perm=true --allow-root
等安装完成,继续回来更。
安装完成了,经历了几次的无故断掉后,终于下载完了,可以试试跑下小东西了。
结果:
(node:11117) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
/home/web/toy/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libXcomposite.so.1: cannot open shared object file: No such file or directory
TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
at onClose (/home/web/toy/node_modules/puppeteer/lib/Launcher.js:349:14)
at Interface.helper.addEventListener (/home/web/toy/node_modules/puppeteer/lib/Launcher.js:338:50)
at Interface.emit (events.js:185:15)
at Interface.close (readline.js:379:8)
at Socket.onend (readline.js:152:10)
at Socket.emit (events.js:185:15)
at endReadableNT (_stream_readable.js:1106:12)
at process._tickCallback (internal/process/next_tick.js:178:19)
f**k, 从来就没顺利的时候,全靠运气...
然后找了下资料,发现缺少依赖库,安装依赖库:
# 依赖库
sudo 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:11792) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
(chrome:11806): Gtk-WARNING **: 15:11:04.104: cannot open display:
TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
at onClose (/home/web/toy/node_modules/puppeteer/lib/Launcher.js:349:14)
at ChildProcess.helper.addEventListener (/home/web/toy/node_modules/puppeteer/lib/Launcher.js:339:60)
at ChildProcess.emit (events.js:185:15)
at Process.ChildProcess._handle.onexit (internal/child_process.js:209:12)
这个问题我这边是沙箱的问题,修改以下代码:
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
// '--no-sandbox' 与 '--disable-setuid-sandbox' 一起使用。
绝望来临了,修复后以上错误都不报错了,但是超时.. 超时
TimeoutError: Navigation Timeout Exceeded: 30000ms exceeded
at Promise.then (/home/web/toy/node_modules/puppeteer/lib/LifecycleWatcher.js:142:21)
at <anonymous>
-- ASYNC --
at Frame.<anonymous> (/home/web/toy/node_modules/puppeteer/lib/helper.js:111:15)
at Page.goto (/home/web/toy/node_modules/puppeteer/lib/Page.js:674:49)
at Page.<anonymous> (/home/web/toy/node_modules/puppeteer/lib/helper.js:112:23)
at /home/web/toy/autosign/sign.js:96:20
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:182:7) name: 'TimeoutError'
不管访问什么地址,百度也好,京东也好,一律超时,但是在我本地是没有问题的,只是在linux
服务器上有这个问题,还在查找验证中...
对chromium做了下简单的优化,提升还是很明显能感觉出来的。
let args = [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
'--no-first-run',
'--no-zygote',
'--single-process',
'--disable-infobars',
];
const browser = await puppeteer.launch({
args : args
});
而且,没想到的是(可能确实是网络问题),增加了几个参数优化了下后,竟然不超时了... [真神奇.jpg]
以上参考于:
转载请注明出处: https://chrunlee.cn/article/nodejs-puppeteer-install-error.html