107

What am I doing wrong when setting up my packages ? Is there any way to speed this up ?

  • packages.json :

    {
    "name": "testing node",
    "version": "0.0.0",
    "description": "",
    "main": "app.config.js",
    "dependencies": {
        "babel-core": "^6.17.0",
        "babel-loader": "^6.2.0",
        "babel-plugin-add-module-exports": "^0.1.2",
        "babel-plugin-react-html-attrs": "^2.0.0",
        "babel-plugin-transform-class-properties": "^6.3.13",
        "babel-plugin-transform-decorators-legacy": "^1.3.4",
        "babel-preset-es2015": "^6.3.13",
        "babel-preset-react": "^6.3.13",
        "babel-preset-stage-0": "^6.3.13",
        "react": "^0.14.6",
        "react-dom": "^0.14.6",
        "webpack": "^1.12.9",
        "webpack-dev-server": "^1.14.1",
        "mysql": "*"
    },
    "devDependencies": {},
    "scripts": {
        "dev": "webpack-dev-server --content-base src --inline --hot",
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC"
    }
    

When inside the folder if I run

npm install

I get the following which can take hours to fully setup:

npm install stuck

This is not a general computing or hardware issue. Comparative speeds are below :

    1. Run haversine to calculate all distances on over 1 million records in a non-index mysql table takes significantly less time. (computational)
    1. Download a full install of Linux (Dual Layer DVD ISO) in significantly less time. (bandwidth)

I suspect there is something wrong with my packages.json or the command I am running npm install. From the image, it seems there are numerous attempts to retrieve the same file. Possibly there is a way to force npm to retrieve from a more stable mirror ? Possible the mirror selection it uses by default is wonky ? Just some suggestions -- I don't know the specific cause which is why I am asking.

This problem also occurs on my Linode, Digital Ocean, and VULTR boxes -- so I suspect it is something specific with npm, the way I am using (something missing), or my packages.json.

Nicolás Alarcón Rapela
  • 2,714
  • 1
  • 18
  • 29
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • Is there any more meaningful output when you do `npm --loglevel=silly install`? –  Jan 07 '17 at 18:27
  • @LINKIWI - will try that -- give me some time to run it. – Kraang Prime Jan 07 '17 at 18:28
  • 13
    @hardillb - not really a problem with my internet or machine speed. Can download a full install of linux faster than `npm install` takes to grab a few scripts -- on the same computer. Comparitively, can run haversine on a non-index recordset in mysql over 1 million records faster. – Kraang Prime Jan 07 '17 at 18:30
  • For what it’s worth, I has similar problems trying to install node from behind a certain firewall. – Tim Grant Jan 07 '17 at 18:46
  • Just waiting on the `npm --loglevel=silly install` -- will paste it on termbin or something. Just seems super slow -- there must be a faster way to get these packages. After install completes, it's usually something no less than 30,000+ files. Does `npm` get each file one by one rather than grab an archive of the package ?? – Kraang Prime Jan 07 '17 at 18:49
  • @TimGrant - No firewall on this box (and some of my 'test' remote boxes i rolled just to test if this issue was local, or consistent). I did notice there is an open unassigned issue [here](https://github.com/npm/npm/issues/11028). Unsure if related, but seems no one there has a solution either. (not for lack of trying) – Kraang Prime Jan 07 '17 at 18:59
  • Which version of npm and nodejs you are using and did you compile those by yourself? – Jari Jokinen Jan 07 '17 at 19:00
  • @JariJokinen - nodejs v0.10.5 , and npm v1.3.0 . I did not compile them, running Ubuntu 14.04.5 LTS – Kraang Prime Jan 07 '17 at 19:03
  • Hmm OK, maybe try complete uninstall before install? (http://stackoverflow.com/questions/11177954/how-do-i-completely-uninstall-node-js-and-reinstall-from-beginning-mac-os-x) The thread you posted includes anecdotes of folks who deleted this or that and got success. – Tim Grant Jan 07 '17 at 19:09
  • 1
    "I suspect there is something wrong with my packages.json or the command I am running npm install" - I don't think so. I installed modules here in less than 1 min with your `package.json` with npm v3.5.2 and node v4.2.6. I suggest you update node and npm. – Andre Figueiredo Jan 07 '17 at 19:09
  • 10
    That is a very, very old version of npm. I wouldn't be surprised if this is a bug in npm that was fixed in a later version. Can you update your npm to latest? `sudo npm install -g npm@latest` –  Jan 07 '17 at 19:11
  • Agree with @LINKIWI, v1.3.0 didn't event had flatten dependencies introduced on v3 that resolved a lot of annoying issues... – Andre Figueiredo Jan 07 '17 at 19:12
  • @LINKIWI - waiting for it to finish still, then will try. That was the version available through `apt`. – Kraang Prime Jan 07 '17 at 19:28
  • @AndreFigueiredo - sounds like it could be a version thing then. Will test out LINKIWI's update method after this. Dunno why the repo for `npm` is such a dated version :S – Kraang Prime Jan 07 '17 at 19:29
  • 1
    Generally speaking, don't rely on package managers like apt to maintain up-to-date software. I would strongly recommend purging the node/npm combo you installed from apt and following the instructions on nodejs.org to install the latest release. –  Jan 07 '17 at 19:54
  • @AndreFigueiredo - purged nodejs and npm, then installed using current version of nvm -- installed nodejs 6.4, which installed npm 3.10.3. Seemed to run much faster around 2 minutes. – Kraang Prime Jan 07 '17 at 21:36
  • @LINKIWI - that command didn't fetch a more current version. I purged npm and nodejs and installed using nvm. npm is much more current now, and runs a lot faster. still takes time, but it's reasonable for fresh install. – Kraang Prime Jan 07 '17 at 21:39
  • hmmm I have this same issue tbqh and I am on a fast connection. Something is up with their server. I tried on my LTE and on my super fast wifi connection. – Chris Jan 07 '17 at 22:30
  • Thank you everyone for assisting with this. It was definitely a problem caused by a severely outdated version of `nodejs` / `npm` thanks to expecting a relatively current version from my various flavors of Linux's package managers. For whatever reason, they have neglected to keep this package current (or relatively current) even though it is one of (if not THE) most dominant web technology today. For those with a similar issue, I posted a solution as I was unable to locate one specific to this as to what to check when `npm` is *slow*, and how to correct it. – Kraang Prime Jan 08 '17 at 04:49
  • there is the `why-npm-i-so-long` package which reports the project dependencies size. – Anton Kastritskiy Jan 14 '20 at 19:08

17 Answers17

56

I was able to resolve this from the comments section; outlining the process below.

From the comments

AndreFigueiredo stated :

I installed modules here in less than 1 min with your package.json with npm v3.5.2 and node v4.2.6. I suggest you update node and npm.


v1.3.0 didn't even have flattened dependencies introduced on v3 that resolved a lot of annoying issues

LINKIWI stated :

Generally speaking, don't rely on package managers like apt to maintain up-to-date software. I would strongly recommend purging the node/npm combo you installed from apt and following the instructions on nodejs.org to install the latest release.

Observations

Following their advice, I noticed that CentOS, Ubuntu, and Debian all use very outdated versions of nodejs and npm when retrieving the current version using apt or yum (depending on operating systems primary package manager).

Get rid of the outdated nodejs and npm

To resolve this with as minimal headache as possible, I ran the following command (on Ubuntu) :

apt-get purge --auto-remove nodejs npm

This purged the system of the archaic nodejs and npm as well as all dependencies which were no longer required

Install current nodejs and compatible npm

The next objective was to get a current version of both nodejs and npm which I can snag nodejs directly from here and either compile or use the binary, however this would not make it easy to swap versions as I need to (depending on age of project).

I came across a great package called nvm which (so far) seems to manage this task quite well. To install the current stable latest build of version 7 of nodejs :

Install nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

Source .bashrc

source ~/.bashrc

Use nvm to install nodejs 7.x

nvm install 7

After installation I was pleasantly surprised by much faster performance of npm, that it also now showed a pretty progress bar while snagging packages.

For those curious, the current (as of this date) version of npm should look like the following (and if it doesn't, you likely need to update it):

current npm running

Summary

DO NOT USE YOUR OS PACKAGE MANAGER TO INSTALL NODE.JS OR NPM - You will get very bad results as it seems no OS is keeping these packages (not even close to) current. If you find that npm is running slow and it isn't your computer or internet, it is most likely because of a severely outdated version.

Suda
  • 234
  • 1
  • 4
  • 20
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • 1
    Nice. [`n`](https://github.com/tj/n) is worth considering as a simpler alternative to `nvm`, installable with `curl -L https://git.io/n-install | bash` – mklement0 Jan 08 '17 at 12:06
  • 1
    I strongly recommend using nvm to tune which version of node you are running. https://github.com/creationix/nvm –  Jun 12 '17 at 23:46
  • 1
    For current releases one can just add a repo, [as specified on the Node.js site](https://nodejs.org/en/download/package-manager/) – E. Sundin Sep 07 '17 at 20:09
  • I concur with nvm recommendation, especially with the --lts flag, which will keep you updated to the latest "long-term support" release of npm, which should be both current, and stable. – moodboom Nov 10 '17 at 15:08
  • Pipe shell script from internet to bash is not good thing to do. – Zdenek Machek Mar 12 '18 at 16:02
  • @ZdenekMachek - I agree. You should review the script before executing. I used that method in the example above as it is the instructions provided for installing that application at the time. – Kraang Prime Mar 13 '18 at 00:41
  • @ZdenekMachek go to raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh to review the code - I did the same :D – Suda Jun 11 '18 at 02:23
  • I think this answer misunderstands the purpose behind OS package managers providing old versions. Most OS's provide versions that are tested to have worked with all the other interacting versions of their software. Other OS's however are less rigid, arch for example and other rolling release OS's provide up-to-date packages with the understanding that things may break. It's a cost/benefit meant for different use-cases and different people. If you want an OS which provides up to date packages, then use the latter type of OS – aaaaaa Mar 05 '21 at 19:42
  • For me, the root cause of the issue was outdated NodeJS/NPM too. In my case, the Linux distribution is SLE 15 SP2. After a couple of days, a NodeJS/NPM update is provided by the SLE which resolved the issue for me. – Megidd Jul 16 '21 at 08:33
16

I am using Linux and have nvm and working with more than 7 version of node As of my experience I experienced the same situation with my latest project (actually not hours but minutes as I can't wait hours because of hourly project :))

Disclaimer: don't try below option until you know how cache clean works

npm cache clean --force

and then all working fine for me so it's looks like sometimes npm's cache gets confused with different versions of Node.

Official documentation of Npm cache can be found here

Abdul Hameed
  • 1,008
  • 1
  • 15
  • 35
  • 1
    I was having issues with the initial "loading" of npm. The first message would stay up for a good 15-30 seconds and then it would blaze by. After running your command, it fixed the issue! Now it starts right away like usual `nodejs v8.11.3` and `npm 5.6.0` on `Windows 7 x64` – Nathan Goings Jul 01 '18 at 03:30
  • 21
    I think adding a brief explanation about how cache clean works (or at least a link) would be better here – Art3mix Dec 13 '18 at 19:00
  • The npm cache is now considered as very stable. Also, I can't imagine how deleting and therefore re-downloading cached data is supposed to speed up npm in any way at all. – Monkey Supersonic Jun 23 '23 at 12:27
9

I see from your screenshot that you are using WSL on Windows. And, with Windows, comes virus scanners, and virus scanning can make NPM install very slow!

Adding an exemption or disabling virus scanning during install can greatly speed it up, but potentially this is undesirable given the possibility of malicious NPM packages

One link suggests triple install time https://ikriv.com/blog/?p=2174

I have not profiled extensively myself though

Colin D
  • 2,822
  • 1
  • 31
  • 38
4

Problem: NPM does not perform well if you do not keep it up to date. However, bleeding edge versions have been broken for me in the past.

Solution: As Kraang mentioned, use node version manager nvm, with its --lts flag

Install it:

curl -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Then use this often to upgrade to the latest "long-term support" version of NPM:

nvm install --lts

Big caveat: you'll likely have to reinstall all packages when you get a new npm version.

moodboom
  • 6,225
  • 2
  • 41
  • 45
4

I was having this problem and none of the solutions in SO helped. I figured it out so I am posting it here in case any one else has a similar issue.

I was trying to run npm i on an amazon instance. The problem ended up being the fact that linux only opens up a certain amount of ports, and when npm i runs, it opens like more than a thousand connects to the registry to download all the packages. So it would work but then just freeze for like 15 minutes. Then the timeout would occur and it would eventually move on to another port. So in my security group in AWS I added a rule for All TCP at 0.0.0.0/0 in outgoing only, letting npm open as many outgoing connections as it likes and that fixed it.

davecoffin
  • 509
  • 1
  • 3
  • 11
2

I was having the same problem, i am on the nodejs version: 8.9.4 and npm version: 5.6.0. I tried a lot solutions online, including the ones on this post, none worked for me, then i found about yarn package manager which solved the problem for me, so if all fails, i think "yarn" is worth checking out.

EDIT:

It seems npm has problems when it is outdated, updating it has helped me as well.

atb00ker
  • 957
  • 13
  • 24
2

I had the same problem on Debian, yarn was the solution for me.

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
aarkerio
  • 2,183
  • 2
  • 20
  • 34
2

This link helped me boost npm installs. Force npm to use http over https and disable progress display.

DK_DEV
  • 51
  • 3
2

Piggybacking on @Colin D's answer about virus scanners and Windows boxes.

There's one more element of Windows that causes NPM to be slow as well. Windows is notoriously heavy when downloading files in general, this is normally not noticed because most of the time we are downloading a single file or zip of files and so we are only downloading from a single source and file.

With npm we are downloading 1000s of files that can get easily into the GBs total download. The way this was described to me in the past, and might be a bit too simplistic, but it made it make sense to me is: Each file that Windows downloads is wrapped in whatever network protocol is being used (TCP) and then run through Windows networking. Usually is negligible, but in the case of these tiny 1kb files it explodes.

Wish I had a more official source for you, so if anyone can help me provide one I'd be grateful. Best I found was this: https://with.raidrive.com/t/very-slow-download-speed-with-small-files/3423/4

For me I'm the source, as we ran into this problem a long time ago when we used to run SVN. It was super slow and we found it was the combination of the network packets and the virus scanner.

Hope this helps.

Kris Boyd
  • 798
  • 10
  • 17
1

we also have had similar problems (we're behind a corporate proxy). changing to yarn at least on our jenkins builds made a huge difference.

there are some minor differences in the results between "npm install" and "yarn install" - but nothing that hurts us.

batman567
  • 826
  • 2
  • 12
  • 23
1

first check for the "VPN" if connected then disconnect the VPN. and now coming to the proper solution : just save whole project and restart your system. you will found that all npm commands works properly.

Monu Rohilla
  • 1,311
  • 5
  • 20
0

I had similar problems. I was also confused by some solutions leaving me with different versions displaying for different users. If you have any problems at all, I would first check every account your using any implementation of node

Finally, this solution appears to solve this issue 100%, giving me the confidence that my versions were universal no matter what user privileges I wanted to use:

    sudo yum update

    sudo yum install build-essential checkinstall libssl-dev

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.1/install.sh | bash

    (sudo) nvm --version

    (sudo) nvm ls-remote

    (sudo) nvm install [version.number] 

1

If you're still having a problem, next try looking inside your /usr/local/bin directory. If you find either a large binary document named node or a folder named npm, delete them.

    rm /usr/local/bin/node

    rm -r /usr/local/bin/npm

Personally, before I deleted those, two of my user accounts were using the latest version of node/npm installed correctly via nvm, while a third account of mine remained stubborn by still using a far older installation of both node and npm apparently located inside my /usr/local/bin/ directory. As soon as I deleted both of them with the two above commands, that problematic user account implicitly began running the correct installation, having all three accounts mutually the intended versions.

(Implemented while using Centos 7 Blank x64. Some source code used above was originally supplied by 'phoenixNAP Global IT services' 1)

0

This didn't work well for me (didn't remove everything):

npm cache clean --force

but this did solve my issue:

rm -rf ~/.npm/
cherouvim
  • 31,725
  • 15
  • 104
  • 153
0

In my case my company VPN was blocking required calls. As soon as I disconnected it started working just fine.

Rider Harrison
  • 441
  • 6
  • 12
0

I'd like to add a possible solution to this. In my case, I had issues in the past where IPV6 requests would take forever to finally fail, I had to disable IPV6 in curl for example. Probably an issue with my ISP. This is why my internet connection appeared to work as fast as ever while some other stuff failed, the stuff that prioritizes IPV6. So I ran this and problem fixed:

sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1
999PingGG
  • 54
  • 1
  • 5
-3

One thing I noticed is, if you are working in new project(folder) you have to reconfigure proxy setting for the particular path

  1. Cd(change terminal window path to the destination folder.

  2. npm config set proxy http://(ip address):(port)

  3. npm config set https-proxy http://(ip address):(port)

  4. npm install -g @angular/cli

tksdotnet1
  • 81
  • 6
  • You should not be setting project specific configuration for global script installations. That configuration would also be distributed with the project's package.json file. – Kennith Nichol Oct 31 '18 at 16:23
-3

install nvm and try it should help, use below command:-

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
zamming
  • 21
  • 6