多个 NPM 和 NodeJS 版本导致 SSL/Certificate 安装 PM2 Profiler 时出现问题

Multiple NPM and NodeJS versions causing SSL/Certificate issues installing PM2 Profiler

我自己解决了这个问题,但没有从网络上的其他答案中找到太多帮助,所以我想我 post 在这里为其他任何人提供我的答案可能有同样的问题。

尝试安装 PM2 探查器来找出导致 NodeJS 应用程序内存泄漏的原因,但在安装过程中出现以下错误:

$ pm2 install profiler
[PM2][Module] Installing module profiler
[PM2][Module] Calling [NPM] to install v8-profiler-node8 ...
npm ERR! Error: CERT_UNTRUSTED
npm ERR!     at SecurePair.<anonymous> (tls.js:1430:32)
npm ERR!     at SecurePair.emit (events.js:92:17)
npm ERR!     at SecurePair.maybeInitFinished (tls.js:1029:10)
npm ERR!     at CleartextStream.read [as _read] (tls.js:521:13)
npm ERR!     at CleartextStream.Readable.read (_stream_readable.js:341:10)
npm ERR!     at EncryptedStream.write [as _write] (tls.js:418:25)
npm ERR!     at doWrite (_stream_writable.js:226:10)
npm ERR!     at writeOrBuffer (_stream_writable.js:216:5)
npm ERR!     at EncryptedStream.Writable.write (_stream_writable.js:183:11)
npm ERR!     at write (_stream_readable.js:602:24)
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Linux 2.6.32-696.20.1.el6.x86_64
npm ERR! command "node" "/usr/bin/npm" "install" "v8-profiler-node8" "--loglevel=error"
npm ERR! cwd /usr/lib/node_modules/pm2
npm ERR! node -v v0.10.48
npm ERR! npm -v 1.3.6
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /usr/lib/node_modules/pm2/npm-debug.log
npm ERR! not ok code 0
[PM2][Module][ERROR] Profiling installation has FAILED (checkout previous logs)
[PM2][ERROR] Module install failed

互联网上有很多 post 建议更改 NPM 设置以忽略证书或忽略 https,但这没有帮助:

https://github.com/nodejs/node/issues/3742#issuecomment-155545828

这是在服务器上 运行 CentOS 6.10

安装中的错误与 SSL 证书有关,但这有点误导。虽然特定错误是由于不受信任的 SSL 证书引起的,但实际上是因为 PM2 安装过程使用的 NodeJS 和 NPM 版本太旧,使用了过时的证书。

在错误中,您可以看到 NodeJS 和 NPM 版本非常旧:

npm ERR! node -v v0.10.48
npm ERR! npm -v 1.3.6

然而,当我在命令行上检查 NodeJS 和 NPM 版本时,它们是更新的:

$ node -v
v7.6.0
$ npm -v
4.1.2

我在安装 NPM 包之前看到过类似的问题,更新 NodeJS/NPM 已经解决了这个问题,但在这种情况下,据我所知我 did 有与 PM2 安装程序尝试使用的版本相比,NodeJS 和 NPM 的更新版本。

这里的关键是查看错误日志并发现:

npm ERR! command "node" "/usr/bin/npm" "install" "v8-profiler-node8" "--loglevel=error"

具体来说,/usr/bin/npm

PM2 使用 /usr/bin 文件夹中的 NPM(我假设是 NodeJS),但是当我使用 which 命令时...

$ which node
/usr/local/bin/node
$ which npm
/usr/local/bin/npm

...我们可以看到更新版本(7.6.0 和 4.1.2)没有安装在 PM2 所在的位置。

当我设置这个服务器时,我可能手动安装了 NodeJS 和 NPM,甚至在 NodeJS 发布之前。

从那时起我就使用 NVM [https://github.com/creationix/nvm] 安装更新的版本。

现在我不知道我的修复是否是解决此问题的最佳方法,但它对我有用。 我删除了 /usr/bin 中的 NodeJS 和 NPM 安装,并添加了指向较新 /usr/local/bin 版本的符号链接。

# check we're in the /usr/bin folder
$ pwd 
/usr/bin

#######################
# SORTING OUT NPM FIRST
#######################

# npm version in the bash environment
$ npm -v 
4.1.2

# npm version for the install at /usr/bin/npm
$ ./npm -v
1.3.6

# get rid of the version here in /usr/bin and add link back to the /usr/local/bin version
$ sudo rm npm
$ sudo ln -s /usr/local/bin/npm npm

# npm version in the bash environment
$ npm -v
4.1.2

# npm version for the install at /usr/bin/npm - now linking to the newer one
$ ./npm -v
4.1.2

##################
# SORTING OUT NODE
##################

# node version in the bash environment
$ node -v
v7.6.0

# node version for the install at /usr/bin/node
$ ./node -v
v0.10.48

# get rid of the version here in /usr/bin and add link back to the /usr/local/bin version
$ sudo rm node
$ sudo ln -s /usr/local/bin/node node

$ node -v
v7.6.0

$ ./node -v
v7.6.0

其实我是先做Node的,也没用(是UNABLE_TO_GET_ISSUER_CERT_LOCALLY而不是CERT_UNTRUSTED的稍微不同的证书错误,然后我整理了NPM。

你也许可以通过整理你的 NPM 安装来做到这一点,但我安装了 Node,然后 NPM 和 PM2 分析器现在安装成功,这就是我给出的答案。