75

我正在尝试在 Ubuntu 中安装 Phonegap。NodeJS 安装成功,但是我无法安装 Phonegap 本身。这是终端的错误输出:

test@test-VirtualBox:~$ sudo npm install -g phonegap
npm http GET https://registry.npmjs.org/phonegap
npm http GET https://registry.npmjs.org/phonegap
npm http GET https://registry.npmjs.org/phonegap
npm ERR! Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE
npm ERR!     at SecurePair.<anonymous> (tls.js:1350:32)
npm ERR!     at SecurePair.EventEmitter.emit (events.js:92:17)
npm ERR!     at SecurePair.maybeInitFinished (tls.js:963:10)
npm ERR!     at CleartextStream.read [as _read] (tls.js:463:15)
npm ERR!     at CleartextStream.Readable.read (_stream_readable.js:320:10)
npm ERR!     at EncryptedStream.write [as _write] (tls.js:366:25)
npm ERR!     at doWrite (_stream_writable.js:219:10)
npm ERR!     at writeOrBuffer (_stream_writable.js:209:5)
npm ERR!     at EncryptedStream.Writable.write (_stream_writable.js:180:11)
npm ERR!     at write (_stream_readable.js:573:24)
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://bugs.debian.org/npm>
npm ERR! or use
npm ERR!     reportbug --attach /home/test/npm-debug.log npm

npm ERR! System Linux 3.11.0-14-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "phonegap"
npm ERR! cwd /home/test
npm ERR! node -v v0.10.15
npm ERR! npm -v 1.2.18
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/test/npm-debug.log
npm ERR! not ok code 0

任何帮助,将不胜感激。

4

6 回答 6

151

我遇到了同样的错误,因为我在公司防火墙/代理后面,并且我的连接通过了代理的证书。

在你的命令行运行:

npm config set strict-ssl false

注意:这不是盲目接受不受信任或无效 SSL 证书的最佳做法,这就是命令的作用(关闭证书检查)。你可以跑

npm config set strict-ssl true

将其重新打开。

参考:https ://thomashunter.name/blog/npm-ssl-errors/

于 2014-01-27T15:51:15.297 回答
31

可以在不禁用严格 SSL 的情况下解决此问题,但这并不重要。

找到实际使用的证书,很可能您在公司 SSL 拦截代理后面。您可能可以使用浏览器、一些 CLI 工具等。我最终certmgr.msc在 Windows 中运行,因为证书是通过组策略分发并导出为 p7b 文件。

如有必要,转换证书,我使用 openssl 工具从 p7b 转换为 PEM(又名 .crt)

openssl pkcs7 -print_certs -inform DER -in /mnt/adam/certs/my-company-root.p7b -outform PEM -out my-company-root.crt

如果有多个证书,则将其合并到一个 PEM 文件中,注意从叶子到根的顺序。

cat my-company-leaf.crt my-company-intermediate.crt my-company-root.crt > my-company-single.crt

在证书文件中配置npm

npm config set cafile my-company-single.crt

(或全球)

sudo npm config set -g cafile my-company-single.crt
于 2015-08-13T11:56:10.427 回答
5

跑步

npm config set strict-ssl false

解决了我的问题。

我正在使用 Vagrant(Linux 精确 32 Ubuntu)和 Windows 7 作为主机。

谢谢

于 2014-02-03T19:11:05.680 回答
2

万一有人像我一样笨拙,当我忘记在我的项目的 url 之前添加时UNABLE_TO_VERIFY_LEAF_SIGNATURE,我就开始了。npm installgit+

我有

npm install --save https://myserv.er/my/project-path.git

代替

npm install --save git+https://myserv.er/my/project-path.git
于 2020-01-14T08:44:13.220 回答
2

您还可以使用节点环境变量在代码中禁用 SSL 检查:

在您的 index.js 文件中,添加:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

请注意,这不是一个好习惯,因为它不会尝试检查 https 证书的有效性

于 2020-06-12T10:02:37.417 回答
0

确保 root 具有配置属性。

使用sudo时,您正在为 root 配置的环境下运行。Root 可能没有相同的节点配置,并且可能不知道您的证书。尝试使用-E将您的环境配置传递给 root :

$ sudo -E npm install -g phonegap
于 2021-03-26T20:01:08.627 回答