为什么“ npm install”真的很慢?

我在设置包时做错了什么?有什么办法能加快进度吗?

  • 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

我得到以下可能需要几个小时才能完全安装:

npm install stuck

这不是一个普通的计算或硬件问题:

    1. 运行 haversine来计算一个非索引 mysql 表中超过100万条记录上的所有距离,所花费的时间要少得多。(计算)
    1. 下载 Linux (双层 DVD ISO)的完整安装,花费的时间要少得多。(带宽)

我怀疑我的 packages.json或者我正在运行的命令 npm install出了问题。从图像来看,似乎有无数次尝试检索同一个文件。也许有一种方法可以强制 npm从一个更稳定的镜像检索?可能它默认使用的镜像选择是不稳定的?只是一些建议——我不知道具体的原因,这就是为什么我要问。

这个问题也发生在我的 Linode、 Digital Ocean 和 VULTR 机顶盒上——所以我怀疑这是 npm特有的东西,我使用的方式(缺少的东西) ,或者我的 packages.json

187501 次浏览

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.

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

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.

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.

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

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

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

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.

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

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)

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.

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

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

npm cache clean --force

but this did solve my issue:

rm -rf ~/.npm/

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.