When using NPM to install a module for a project, you may encounter a frustrating error like this:
$ npm install express --save
npm http GET https://registry.npmjs.org/express
npm ERR! Error: failed to fetch from registry: express
npm ERR! at /opt/node0610/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:139:12
npm ERR! at cb (/opt/node0610/lib/node_modules/npm/lib/utils/npm-registry-client/request.js:32:9)
npm ERR! at Request._callback (/opt/node0610/lib/node_modules/npm/lib/utils/npm-registry-client/request.js:137:18)
npm ERR! at Request.callback (/opt/node0610/lib/node_modules/npm/node_modules/request/main.js:109:22)
npm ERR! at Request.<anonymous> (/opt/node0610/lib/node_modules/npm/node_modules/request/main.js:198:58)
npm ERR! at Request.emit (events.js:88:20)
npm ERR! at ClientRequest.<anonymous> (/opt/node0610/lib/node_modules/npm/node_modules/request/main.js:195:10)
npm ERR! at ClientRequest.emit (events.js:67:17)
npm ERR! at CleartextStream.<anonymous> (http.js:1134:11)
npm ERR! at CleartextStream.emit (events.js:67:17)
npm ERR! You may report this log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <[email protected]>
npm ERR!
npm ERR! System Linux 2.6.18-194.el5
npm ERR! command "node" "/opt/node0610/bin/npm" "install" "express"
npm ERR! cwd /opt/node0610/lib/node_modules
npm ERR! node -v v0.6.10
npm ERR! npm -v 1.1.0-3
npm ERR! message failed to fetch from registry: express
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /opt/node0610/lib/node_modules/npm-debug.log
npm not ok
As I have found out in the past, there are quite a few reasons why you might get the "failed to fetch from registry" error, so here are a few ways to fix it.
Old/Buggy NPM Version
As you might have noticed from the error message above, the npm version being used is pretty old. In this example we were using Node v0.6.10 and npm v1.1.0-3. This version is known to have some problems with Ubuntu 12.04, so your best bet is to update Node (and npm along with it) to the newest version:
$ sudo npm update npm -g
If you want to re-install completely, you'll first want to completely remove the current Node/npm executables:
$ sudo apt-get purge nodejs npm
Then re-install using a more updated version, like from Nodesource:
$ curl -sL https://deb.nodesource.com/setup | sudo bash -
$ sudo apt-get install -y nodejs
Or, even better, you can check out this article for a more thorough guide to installing Node.js on Ubuntu.
Can't Download Over HTTPS
For one reason or another, some people can't connect to the registry via HTTPS. This can be fixed by setting the registry to use HTTP instead:
$ npm config set registry http://registry.npmjs.org/
$ npm config set strict-ssl false
This isn't recommended, however, since your packages will then be downloaded insecurely. It would be best to find the actual root cause instead of using a work-around like this.
Corporate Proxy
For many people, the root cause is actually because a proxy is being used on their network. If this is the case, you can use the following commands to set the HTTP and HTTPS proxies:
$ npm config set proxy http://user:[email protected]:8181
$ npm config set https-proxy http://user:[email protected]:8181
This will save the new configurations and should allow you to access the internet with npm.