Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "npm install" really slow?

Tags:

node.js

npm

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.

like image 935
Kraang Prime Avatar asked Jan 07 '17 18:01

Kraang Prime


People also ask

How long does npm install usually take?

it takes ~5 minutes to download and install.

Why npm install is slow in Windows?

It seems like npm do not deal well with internet connections and can sometimes end in such situation. 1) I'll advice you to check if your firewall or antivirus is not performing any scan or filtering which may slow down npm install. 2) You may also use caching from npm like below.

Why npm install hangs?

Check your network connection Even though the previously installed packages are cached, npm will still connect to the registry using your network connection when there are dependencies not found in the cache. If you are offline, then the installation could hang in the middle of the process.


2 Answers

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.

like image 183
Kraang Prime Avatar answered Sep 18 '22 17:09

Kraang Prime


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

like image 40
Abdul Hameed Avatar answered Sep 21 '22 17:09

Abdul Hameed