364

I can't install any node module from the npm.

npm install socket.io

The above command resulted to below output, it is not able to install socket.io

npm http GET https://registry.npmjs.org/socket.io

npm ERR! Error: failed to fetch from registry: socket.io
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!     <npm-@googlegroups.com>
npm ERR! 
npm ERR! System Linux 2.6.18-194.el5
npm ERR! command "node" "/opt/node0610/bin/npm" "install" "socket.io"
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: socket.io
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /opt/node0610/lib/node_modules/npm-debug.log
npm not ok

My NPM Version is :

[applmgr@dev node_modules]$ npm --version
1.1.0-3

My NodeJS Version is :

[applmgr@dev node_modules]$ node --version
v0.6.10
Braiam
  • 1
  • 11
  • 47
  • 78
Dexter
  • 12,172
  • 9
  • 27
  • 30
  • 12
    That's a pretty old `npm` version. Update it to the latest and try again: `sudo npm update npm -g` – JohnnyHK Oct 16 '12 at 12:27
  • 1
    johny thanks for your comment, actually if i need to update npm should I also update nodejs – Dexter Oct 16 '12 at 12:51
  • 1
    You don't have to update nodejs as well, but I would recommend it if you're not tied to the 6.x version for some reason. – JohnnyHK Oct 16 '12 at 12:53
  • HTTP works; HTTPS doesn't, because registry.npmjs.org isn't listening for HTTPS. – Mark Feb 03 '14 at 22:43
  • 1
    I usually download & compile the latest version of nodejs from [official site](http://nodejs.org/). Distributions I use (usually Ubuntu LTS) have too old version. – Alex Lokk Feb 27 '14 at 11:06
  • Future readers of this thread: look *below* the accepted answer. This is an unconventional SO thread where there's significantly more to say than just the ✓ solution. – isomorphismes Oct 25 '14 at 14:09
  • Voir aussi http://stackoverflow.com/a/27675564/1974961 – Hugolpz Dec 28 '14 at 12:28

15 Answers15

804

I had this issue with npm v1.1.4 (and node v0.6.12), which are the Ubuntu 12.04 repository versions.

It looks like that version of npm isn't supported any more, updating node (and npm with it) resolved the issue.

First, uninstall the outdated version (optional, but I think this fixed an issue I was having with global modules not being pathed in).

sudo apt-get purge nodejs npm

Then enable nodesource's repo and install:

curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs

Note - the previous advice was to use Chris Lea's repo, he's now migrated that to nodesource, see:

From: here

John Carter
  • 53,924
  • 26
  • 111
  • 144
  • 13
    Note that the nodejs package includes npm and nodejs-dev, so installing nodejs from the ppa gets you everything needed for npm. – chawkinsuf Feb 25 '14 at 01:20
  • 1
    Worked, best solution I think as well, and obviously better than having to download the source code from the official page and compile everything. – htatche Mar 10 '14 at 16:19
  • I used this when `node-gyp` from Ubuntu 14.04 was failing because of `gyp --no-parallel` not supported, doing these steps and all is working fine again (I guess it had to do with the upgrade from 13.10, the updater disables the ppa's, not sure) – Sebastian Apr 25 '14 at 20:21
  • 2
    Thanks! This worked for me! This would be the accepted answer for "Ask Ubuntu" but it seems that plenty non-Ubuntu users find the accepted answer to be helpful. – dannyman Jun 18 '14 at 18:25
  • 1
    I think the issue has to do with the fact that npm switched ssl certs; the 0.6.10 version didn't "trust" GlobalSign, who appears to be the current CA in the cert chain for npmjs. Latest versions clearly do.. Read more about it here: https://news.ycombinator.com/item?id=7322950 – RoyM Jul 10 '14 at 21:48
  • I figured I needed to upgrade node after seeing I had an older version. Then I saw your answer. You're spot on. – Natus Drew Oct 13 '14 at 04:55
  • This answer worked for me where the accepted answer did not, but I recognize that for the 450+ other people who upvoted it, it probably did. – Dan Nov 03 '14 at 22:04
  • 1
    This used to work, but the ppa is no longer maintained: https://nodesource.com/blog/chris-lea-joins-forces-with-nodesource – mbursill Nov 14 '14 at 02:03
  • 2
    ran this on raspbery pi and got Segmentation fault when running nodejs or npm – godzilla Jan 14 '15 at 06:24
  • 5
    After using these instructions and getting a Segmentation fault on raspberry pi (Raspbian) I removed the port and followed these instructions https://gist.github.com/justinjenkins/16adc15338bb265f6808 now it works! – Justin Jenkins Jan 16 '15 at 03:03
  • I'm getting 404s now, and it's failing to install? Does this need updating? – Phil Hudson Mar 01 '15 at 17:29
  • +1 for the better sln. grr. this voting madness. it's taking an age to promote this as the top answer -never mind the fact the OP can't be bothered to re-accept. grr. why can't folk understand wot the voting is actually designed for. humans r so egocentric fools. /sighs..long/ – violet313 May 07 '15 at 19:07
  • You should now use this command (for the LTS version), as noted when you try the other: curl -sL https://deb.nodesource.com/setup_4.x | sudo bash - – DylanYoung Jul 27 '16 at 16:33
  • 2
    -1 for recommending that unsuspecting newcomers do something as unwise and unsafe as *piping code from an unverified source on the internet to a root shell*. Irresponsible hardly covers it. You should at least have a caveat about safety if not a recommendation to verify a checksum. – sorpigal May 23 '17 at 01:49
  • If you want to install node 6.x: `curl -sL https://deb.nodesource.com/setup_6.x | sudo bash -` – Akira Yamamoto Jan 10 '18 at 08:49
651

https://github.com/isaacs/npm/issues/2119

I had to execute the command below:

npm config set registry http://registry.npmjs.org/

However, that will make npm install packages over an insecure HTTP connection. If you can, you should stick with

npm config set registry https://registry.npmjs.org/

instead to install over HTTPS.

isomorphismes
  • 8,233
  • 9
  • 59
  • 70
Dexter
  • 12,172
  • 9
  • 27
  • 30
  • 7
    Using a raspberry pi, and this is the same command I had to use. changed https://registry.npmjs.org/pi-gpio to http://registry.npmjs.org/pi-gpio. Not sure whats up with the https not accessible by the pi – Jdahern Dec 28 '13 at 19:16
  • Could someone please explain why this works? Thanks in advance. – SleepyCal Jan 28 '14 at 20:26
  • 1
    If you visit https://registry.npm.org (which is what my config registry was set to), you'll see that the web page is not available. – jevon Jan 29 '14 at 00:09
  • If this doesn't do the trick the first time, try doing it as sudo. – Rob Gibbons Jan 29 '14 at 03:31
  • 28
    Warning: this pulls the resources in over an /insecure/ connection. Your files may be changed before you get them. Again: this is not secure. – Lodewijk Feb 07 '14 at 16:48
  • I friggin love this site! it would have taken me hours to figure this out, Stack Overflow (1 minute). – Chris Hawkes Feb 08 '14 at 19:21
  • 38
    Do not use this unsecure solution, even if it works. The apt-get version of Node.js is too old (0.6.12) ; I installed the latest stable version from GitHub and it solved the problem. – Louis Ameline Feb 12 '14 at 09:55
  • 2
    It won't work for some other nodejs packages. I'm just downloading newer version of node.js from [official site](http://nodejs.org/) and compiling it by hand. – Alex Lokk Feb 27 '14 at 11:10
  • As noted by others, this solution installs packages over an *insecure* connection - meaning it is possible for a malicious party to intercept the download and inject their own code. The proper solution is to install an updated version of `npm` that correctly establishes an `https` connection. – Shawn South Apr 03 '14 at 20:07
  • Every time I set up a new node server I forget to do this. Thank you for putting this here so I can keep referencing it! – HarshMarshmallow Jul 22 '14 at 20:55
  • 1
    @Lodewijk: I changed the URL to a `https://` URL instead. You can always propose an edit to a post to fix problems like this. – Martin Geisler Oct 10 '14 at 09:00
  • Not a solution but a workaround. Realo solution to the problem is the answer below. http://stackoverflow.com/a/21715730/1019876 – wouterds Feb 15 '15 at 11:54
  • This is the right solution - http://stackoverflow.com/questions/12913141/message-failed-to-fetch-from-registry-while-trying-to-install-any-module/24012652#24012652 – Tushar Sep 16 '15 at 07:03
  • This solution doesn't work in my case. I did use apt-get from raspberry pi and the version of node is still very old. I guess upgrading the node/npm is the best solution. – fiorentinoing Dec 08 '15 at 11:05
32

I'm on Ubuntu. I used apt-get to install node. Npm was not included in that package, so it had to be installed separately. I assumed that would work, but apparently the npm version in the Ubuntu distribution was outdated.

The node wiki has this instruction:

Obtaining a recent version of Node or installing on older Ubuntu and other apt-based distributions may require a few extra steps. Example install:

sudo apt-get update
sudo apt-get install -y python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

After that, npm was already included and worked perfectly.

geon
  • 8,128
  • 3
  • 34
  • 41
19

For me, it's usually a proxy issue, and I try everything:

npm config set registry http://registry.npmjs.org/
npm config set strict-ssl false

npm config set proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set https-proxy http://myusername:mypassword@proxy.us.somecompany:8080
set HTTPS_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
set HTTP_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export HTTPS_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export HTTP_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export http_proxy=http://myusername:mypassword@proxy.us.somecompany:8080

npm --proxy http://myusername:mypassword@proxy.us.somecompany:8080 \
--without-ssl --insecure -g install
Katie
  • 45,622
  • 19
  • 93
  • 125
17

You also need to install software-properties-common for add-apt-repository to work. so it will be

sudo apt-get purge nodejs npm
sudo apt-get install -y python-software-properties python g++ make software-properties-common
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
Tushar
  • 441
  • 4
  • 13
10

One thing that has worked for me with random npm install errors (where the package that errors out is different under different times (but same environment) is to use this:

npm cache clean

And then repeat the process. Then the process seems to go smoother and the real problem and error message will emerge, where you can fix it and then proceed.

This is based on experience of running npm install of a whole bunch of packages under a pretty bare Ubuntu installation inside a Docker instance. Sometimes there are build/make tools missing from the Ubuntu and the npm errors will not show the real problem until you clean the cache for some reason.

Edward Tan
  • 934
  • 9
  • 17
7

This problem is due to the https protocol, which is why the other solution works (by switching to the non-secure protocol).

For me, the best solution was to compile the latest version of node, which includes npm

apt-get purge nodejs npm
git clone https://github.com/nodejs/node ~/local/node
cd ~/local/node
./configure
make
make install
Lawrence Kesteloot
  • 4,149
  • 2
  • 31
  • 28
Venar303
  • 313
  • 3
  • 6
6

There are now official instructions from joyent (primary nodejs backer). For Ubuntu:

sudo apt-get purge nodejs npm
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs

For other unix distributions, osx and windows see the link. Note this will install both node and npm.

Brian Low
  • 11,605
  • 4
  • 58
  • 63
5

The only thing that worked for me on Elementary OS Luna, a Ubuntu Fork. I am on x86 architecture. I tried all the answers here but finally decided to install it from source.

First, make sure its not installed using the package manager:

sudo apt-get purge nodejs npm -y

I went to the download page to lookup the latest source & download it, http://nodejs.org/download/. You can use curl, wget or your browser to get it:

wget http://nodejs.org/dist/v0.10.34/node-v0.10.34.tar.gz
tar -xvf node-v0.10.34.tar.gz 
cd node-v0.10.34
./configure
make
sudo make install

The make might take a while. When done, you should have node and npm installed and working in your /usr/local/bin directory which should be already on your path. You should verify where it lives:

which npm node

I also had to change the permissions to get it to work:

sudo chown -R $USER /usr/local

If it didn't work check your path:

echo $PATH

Note that installing it this way, it will not be managed by apt-get package manager. Cheers!

radtek
  • 34,210
  • 11
  • 144
  • 111
3

Recently I had this problem after upgrading node.js (and inevitably npm) to the newest version:

> npm --version < 2.0.0-alpha-5

Note: I didn't ask for an unstable version, I just got it after brew install npm on OSX.

Downgrading npm fixed the problem for me.

The easiest way to install the stable npm is npm install -g npm but it might not work under some circumstances and downgrade of node.js might be needed then.

Michał Miszczyszyn
  • 11,835
  • 2
  • 35
  • 53
2

The below method worked for me, Kudos to github user : midnightcodr

Make sure You remove any nodejs/npm packages already installed.

sudo apt-get purge nodejs

sudo apt-get purge npm

Now Install Node js using the command below( Thanks to midnightcodr on github)

curl -L https://raw.github.com/midnightcodr/rpi_node_install/master/setup.sh | bash -s 0.10.24

Note that you can invoke node with command node and not nodejs.

Once node is installed , Install npm

sudo apt-get install npm
Eswar Rajesh Pinapala
  • 4,841
  • 4
  • 32
  • 40
1

It could be that the npm registry was down at the time or your connection dropped.

Either way you should upgrade node and npm.

I would recommend using nave to manage your node environments.

https://npmjs.org/package/nave

It allows you to easily install versions and quickly jump between them.

serby
  • 4,186
  • 2
  • 24
  • 25
1

for raspberry pi I found and modified a solution I found
here is what I ran

sudo su -
cd /opt
wget http://nodejs.org/dist/v0.10.28/node-v0.10.28-linux-arm-pi.tar.gz
tar xvzf node-v0.10.28-linux-arm-pi.tar.gz
ln -s node-v0.10.28-linux-arm-pi node
chmod a+rw /opt/node/lib/node_modules
chmod a+rw /opt/node/bin
echo 'PATH=$PATH:/opt/node/bin' > /etc/profile.d/node.sh

the only mod I did was change all 10.25 to 10.28 which was the latest linux-arm-pi at the time

godzilla
  • 971
  • 1
  • 10
  • 18
0

@therefromhere's answer is the best one. However Node versions have moved ever onwards and upwards, and the versioning was complicated by the remerge of io.js. Following the steps in his answer, you will end up with Node version 0.10.25 - not the most recent version.

You should still purge any existing node/npm packages with

sudo apt-get purge nodejs npm

and then go and look at the nodesource deb install page at https://github.com/nodesource/distributions#debinstall.

All Node.js versions are listed on the above page with the Linux commands to install them. And these packages will work on both Debian and Ubuntu.

P.S. If you want to run Node v4.4 or higher on Ubuntu Precise or Debian Wheezy, you should review the information about running on older distros.

P.P.S. If your apt-get is failing, the script will not complete (Google references were failing my apt-get update). You should see messages about the NodeSource signing key being added to your keyring and a final message instructing you to Run 'apt-get install nodejs' to install Node.js.

P.P.P.S. npm is installed with node. Once installed, you can update to the latest npm with sudo npm install npm -g

timbo
  • 13,244
  • 8
  • 51
  • 71
0

For people having this issue from a company network which implement a security system that use a mitm for https traffic, you can configure npm to use your company certificate.

npm config set cafile 'path/to/certif-file'

See https://docs.npmjs.com/cli/v9/using-npm/config#cafile

But don't use configs like strict-ssl=false or setting the registry to http://. This is not a secure way of working.

ylerjen
  • 4,109
  • 2
  • 25
  • 43