Search code examples
node.jsnpmpm2

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


I've worked this out myself but didn't find much help from other answers from around the net, so I thought I'd post my answer here for anyone else that might have the same problem.

Trying to install the PM2 profiler to work out what is causing a memory leak in a NodeJS application, but during installation I'm getting the following error:

$ 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

Lots of posts around the internet suggest changing NPM settings to ignore the certificates or ignore https but this hasn't helped:

https://stackoverflow.com/a/45884819/884842 https://github.com/nodejs/node/issues/3742#issuecomment-155545828

This is on a server running CentOS 6.10


Solution

  • The errors in the installation are related to SSL Certificates, but this is a bit misleading. While the specific error is due to untrusted SSL certificates, it's really because the versions of NodeJS and NPM being used by the PM2 installation process are old, which use outdated certificates.

    In the error you can that the NodeJS and NPM versions were very old:

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

    However when I check the NodeJS and NPM versions on the command line they are more recent:

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

    I've seen similar issues before installing NPM packages, updating NodeJS/NPM has fixed the issue, but in this case as far as I could tell I did have a newer version of both NodeJS and NPM, compared to what the PM2 installer was trying to use.

    The key thing here, was looking in the error log and spotting this:

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

    Specifically, /usr/bin/npm

    PM2 is using NPM (and I assume NodeJS) from the /usr/bin folder, but when I use the which command...

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

    ...we can see that the newer versions (7.6.0 and 4.1.2) are not installed where PM2 is looking.

    When I set this server up I probably installed NodeJS and NPM manually, way back before NodeJS was even released.

    Since then I've use NVM [https://github.com/creationix/nvm] to install newer versions.

    Now I don't know if my fix is the best way to sort this out, but it worked for me. I deleted the NodeJS and NPM installs in /usr/bin and added Symbolic links to the newer /usr/local/bin versions.

    # 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
    

    I actually did Node first, and it didn't help (it was a slightly different certificate error of UNABLE_TO_GET_ISSUER_CERT_LOCALLY instead of CERT_UNTRUSTED, then I sorted out NPM.

    You might be able to do it by just sorting out your NPM install, but I did Node then NPM and PM2 profiler is now installed successfully, so that's the answer I'm giving.