67

My npm is getting stuck a lot, whether its npm install from package.json or individual packages.

Right now, I'm not even able to sudo npm install node-inspector -g

I'm using node 6.2.1, npm 3.9.5 on ubuntu 16.04

npm cache clear didn't help. The installation takes hours or fails.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80

11 Answers11

63

Check if there any issues with proxies if you using any. You can try also to set npm's endpoint manually:

npm config set registry="http://registry.npmjs.org"
G07cha
  • 4,009
  • 2
  • 22
  • 39
  • 3
    @Brett84c, thanks for the feedback, I've updated my answer! – G07cha Dec 29 '16 at 21:13
  • This didn't help me. Had to use yarn install – master_dodo Feb 12 '17 at 15:07
  • can you please explain when this config gets changed. I mean in my case one of my repo has .npmrc which points to a different registry and npm install works there. but when I changed to another repo where there is no .npmrc it should fetch from http://registry.npmjs.org, right? If I am not wrong – prazor9 Jun 09 '18 at 12:53
  • If there is no .npmrc file presented, npm will read values from global config which could be set by the command in my answer, see [npm-config priority](https://docs.npmjs.com/misc/config) – G07cha Jun 10 '18 at 20:20
34

try yarn installer instead of npm:

sudo npm install -g yarn
yarn install

I found it much more resilient to such problems.

Tomer Ben David
  • 8,286
  • 1
  • 43
  • 24
  • 2
    @ssethupathi explained this in more detail. Basically, yarn is an alternative CLI utility to npm. You can still use your `package.json` file to declare your dependencies, and these dependencies will still be installed in `node_modules`. – yegeniy Apr 28 '17 at 19:06
  • Just for anyone who saw this, yarn preferred installation method is here: https://yarnpkg.com/en/docs/install – Lionel Chan Jan 29 '18 at 03:09
  • how this command will work if npm install iteself is not working? – shijin Nov 28 '18 at 18:10
  • Yarn seems to be handling connectivity issues better. – Vaiden Dec 27 '20 at 17:51
23

You'll need to clear your https proxy settings:

npm config rm proxy
npm config rm https-proxy
daniel aagentah
  • 1,672
  • 5
  • 29
  • 56
  • 1
    tqsm buddy! I have been struck at this from 4 straight hours! and about to lose all hope but your answer solved my problem! tq a lot :) – p u May 29 '19 at 10:20
  • I don't see why that should necessarily helps if you are in fact using a proxy. I received this exception on a specific npm package, whereas I had no problems installing another... – html_programmer Jan 27 '21 at 12:14
  • thank you so much! this really saved me. I tried a lot of configs and any worked for me. Now a question raises, what's the difference between config "rm" and config "delete"? – Migerusantte Oct 27 '21 at 18:16
19

I had this same problem, with several different installs hanging on fetchMetaData, including the install of yarn mentioned above. I am on a Mac and solved it by updating npm as follows:

npm install npm@latest -g

So if you are encountering this issue on Mac, then try updating and see if it solves your problem.

BruceHill
  • 6,954
  • 8
  • 62
  • 114
10

Did you configured a proxy? See if this returns something:

npm config get https-proxy
npm config get proxy

If you have values configured there, they might not match correct ones. If you shouldn't be using a proxy, you can delete them:

npm config delete https-proxy
npm config delete proxy

Docs: https://docs.npmjs.com/cli/config

Ali Pardhan
  • 194
  • 1
  • 14
Paul Melero
  • 1,323
  • 15
  • 15
6

For me it was double temp directories in Windows.

Open a command prompt and type:

echo %TEMP%

You should receive path to a single directory. If you receive multiple directories (I received C:\Users\\AppData\Local\Temp;C:\xampp\php) this might be the problem. You can fix it using this command:

SET TEMP="<correct path to temporary directory>"

where <correct path to temporary directory> might be the first path (before semicolon, that means C:\Users\<user>\AppData\Local\Temp in my case).

Ogglas
  • 62,132
  • 37
  • 328
  • 418
4

There are a lot of different things that can cause this issue.

I had tried a lot of things, including most of the answers posted here, but no luck.

For some weird reason, this was my ISP's issue. It worked fine when I tried on a different ISP.

Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80
  • 5
    Before someone goes ahead and downvotes this, please note that I'm the one who posted the question, and this is what worked for me. The other answers are definitely helpful for a lot of people, but this is what worked for me. – Dushyant Bangal May 22 '18 at 10:33
3

I got the same issue. First of all try to run npm install with -verbose flag. That can help you identify your problem.

That command told me that I had some problem with tunneling socket:

tunneling socket could not be established, cause=write EPROTO 101057795:error:140770FC:SSL routines:SSL 23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:794:

After that I googled the error and found thread on github about that problem. The problem was in proxy: previously I set up HTTP_PROXY environment variable and after removing then environment variable npm install completed without any error.

feeeper
  • 2,865
  • 4
  • 28
  • 42
  • Awesome man! Identified issue by -verbose flag. This is proxy mistake. I change my proxy to null now working fine. – Vasi Aug 13 '17 at 11:21
3

I had a similar issue to @giridhar but it wasn't related to the proxy, just that my ssh key wasn't setup properly. When trying to do a git ls-remote from an ssh:// url. I was getting the prompt to enter my passphrase for my ssh key.

I had to run these commands on command line in order to stop getting asked for my pass phrase:

$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/id_rsa

Once you enter your pass phrase, then you don't have to enter it again anymore.

I highly recommend reading this wiki from Atlassian if you need to setup an ssh key: https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html

Daniel Tome
  • 228
  • 1
  • 4
1

Having same problem. Fixed it by switching to correct version of node required by the project.

My project needed node >=v4.3 and <=v5.

1) Check node version being used by nvm ls

2) Use nvm use [version] or else install the specific version. if you don't have.

master_dodo
  • 1,203
  • 3
  • 20
  • 31
  • similar for me, when I ran nvm list I saw that my node version was set to 'system' for some reason. I changed it to an installed node version and everything worked again. – Louis Sankey Apr 29 '23 at 04:04
1

For the major chunk of the people, the issue could be related to proxy settings and can be solved by the above-mentioned solutions.

For me, the issue was something else. I have private repository dependencies which are hosted at Bitbucket and are resolved through "git+ssh://git@bitbucket.org/" which intern uses SSH to retrieve the repo.

SSH by default prefers IPv6 if DNS retrieves AAAA record and if the host has one. Since Bitbucket does have AAAA record, SSH is done over IPv6. But my ISP doesn't support IPv6 (You could check if your ISP support's IPv6 by taking a test in http://ipv6-test.com/).

To solve this, you could force SSH client to use IPv4 instead of IPv6.

Set AddressFamily inet in /etc/ssh/ssh_config and restart SSH client.

On Ubuntu to restart SSH client use sudo service ssh restart

giridhar
  • 141
  • 1
  • 7