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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41524903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:54:55  来源:igfitidea点击:

Why is "npm install" really slow?

node.jsnpm

提问by Kraang Prime

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"
    }
    
  • 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

npm 安装卡住了

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

这不是一般的计算或硬件问题。比较速度如下:

    1. Run haversineto 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)
    1. 运行haversine以计算非索引 mysql 表中超过 100 万条记录的所有距离花费的时间显着减少。(计算)
    1. 在更短的时间内下载完整安装的 Linux(双层 DVD ISO)。(带宽)

I suspect there is something wrong with my packages.jsonor 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 npmto 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.

我怀疑我的packages.json或我正在运行的命令有问题npm install。从图像上看,似乎有多次尝试检索相同的文件。可能有一种方法可以强制npm从更稳定的镜子中检索?它默认使用的镜像选择可能是不稳定的?只是一些建议——我不知道具体的原因,这就是我问的原因。

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.

这个问题也出现在我的 Linode、Digital Ocean 和 VULTR 盒子上——所以我怀疑它是特定的npm,我使用的方式(缺少某些东西),或者我的packages.json.

采纳答案by Kraang Prime

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

我能够从评论部分解决这个问题;概述下面的过程。

From the comments

来自评论

AndreFigueiredostated :

AndreFigueiredo说:

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.

我用 npm v3.5.2 和 node v4.2.6 用 package.json 在不到 1 分钟的时间内在这里安装了模块。我建议你更新 node 和 npm。



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

v1.3.0 甚至没有在 v3 上引入的扁平化依赖解决了很多烦人的问题

LINKIWIstated :

LINKIWI表示:

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.

一般来说,不要依赖像apt这样的包管理器来维护最新的软件。我强烈建议清除您从 apt 安装的 node/npm 组合,并按照 nodejs.org 上的说明安装最新版本。

Observations

观察

Following their advice, I noticed that CentOS, Ubuntu, and Debian all use veryoutdated versions of nodejsand npmwhen retrieving the currentversion using aptor yum(depending on operating systems primary package manager).

按照他们的建议,我注意到 CentOS、Ubuntu 和 Debian 都使用非常过时的版本,nodejs并且npm在使用or检索当前版本时(取决于操作系统的主包管理器)。aptyum

Get rid of the outdated nodejsand npm

摆脱过时nodejsnpm

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

为了尽可能少地解决这个问题,我运行了以下命令(在 Ubuntu 上):

apt-get purge --auto-remove nodejs npm

This purged the system of the archaic nodejsand npmas well as all dependencies which were no longer required

这清除了陈旧的制度nodejsnpm以及它已不再需要所有的依赖

Install current nodejsand compatible npm

安装当前的nodejs和兼容的npm

The next objective was to get a current version of both nodejsand npmwhich I can snag nodejsdirectly from hereand 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).

下一个目标是让两者的当前版本nodejsnpm我可以嵌入nodejs直接从这里,要么编译或使用二进制,但是这不会很容易地交换的版本,因为我需要(取决于项目的年龄)。

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

我遇到了一个叫做nvm的很棒的包,它(到目前为止)似乎很好地管理了这个任务。要安装当前稳定的最新版本 7 的版本nodejs

Install nvm

安装 nvm

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

Source .bashrc

来源 .bashrc

source ~/.bashrc

Use nvm to install nodejs7.x

使用 nvm 安装nodejs7.x

nvm install 7

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

安装完毕后,我惊喜惊讶更快的性能npm,它现在也表现出了漂亮的进度条,而抽丝包。

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

对于那些好奇的人,当前(截至目前)版本npm应如下所示(如果不是,您可能需要更新它):

current npm running

当前 npm 正在运行

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 npmis running slowand it isn't your computer or internet, it is most likely because of a severelyoutdated version.

不要使用你的操作系统包管理器来安装 NODE.JS 或 NPM- 你会得到非常糟糕的结果,因为似乎没有操作系统保持这些包(甚至不接近)最新。如果您发现npm正在运行速度慢,这是不是您的计算机或网络,这是因为一个最有可能严重过时的版本。

回答by Abdul Hameed

I am using Linux and have nvmand working with more than 7 version of nodeAs 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 :))

我正在使用 Linux 并且拥有nvm和使用超过 7 个版本的节点根据我的经验,我在我的最新项目中遇到了同样的情况(实际上不是几个小时而是几分钟,因为我不能等待几个小时,因为每小时的项目:))

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

免责声明:在您了解缓存清理工作原理之前,请勿尝试以下选项

npm cache clean --force

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.

然后对我来说一切正常,所以看起来有时 npm 的缓存会与不同版本的 Node.js 混淆。

Official documentation of Npm cache can be foundhere

Npm 缓存的官方文档可以在这里找到

回答by moodboom

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.

问题:如果您不保持最新状态,NPM 不会表现良好。然而,过去的前沿版本对我来说已经被打破了。

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

解决方案:正如 Kraang 提到的,使用节点版本管理器nvm及其 --lts 标志

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:

然后经常使用这个来升级到最新的“长期支持”版本的 NPM:

nvm install --lts

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

大警告:当您获得新的 npm 版本时,您可能必须重新安装所有软件包。

回答by Colin D

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!

我从您的屏幕截图中看到您在 Windows 上使用 WSL。而且,Windows 自带病毒扫描程序,病毒扫描会使 NPM 安装速度非常慢!

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

在安装过程中添加豁免或禁用病毒扫描可以大大加快速度,但鉴于恶意 NPM 包的可能性,这可能是不可取的

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

一个链接建议三重安装时间https://ikriv.com/blog/?p=2174

I have not profiled extensively myself though

虽然我没有广泛地介绍自己

回答by davecoffin

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.

我遇到了这个问题,SO 中的任何解决方案都没有帮助。我想通了,所以我把它贴在这里,以防其他人有类似的问题。

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.

我试图在亚马逊实例上运行 npm i 。问题最终是这样一个事实,即 linux 只打开了一定数量的端口,当 npm i 运行时,它会打开一千多个连接到注册表以下载所有包。所以它会起作用,但然后只是冻结 15 分钟。然后会发生超时,它最终会移动到另一个端口。因此,在 AWS 的安全组中,我仅在传出中为 0.0.0.0/0 处的所有 TCP 添加了一条规则,让 npm 打开任意数量的传出连接并修复它。

回答by DK_DEV

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

这个链接帮助我提升了 npm installs。强制 npm 通过 https 使用 http 并禁用进度显示。

回答by aarkerio

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

我在 Debian 上遇到了同样的问题,yarn 是我的解决方案。

回答by atb00ker

I was having the same problem, i am on the nodejs version: 8.9.4and 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 managerwhich solved the problem for me, so if all fails, i think "yarn" is worth checking out.

我遇到了同样的问题,我在nodejs version: 8.9.4和 上npm version: 5.6.0。我在网上尝试了很多解决方案,包括这篇文章中的解决方案,没有一个对我有用,然后我找到了纱线包管理器,它为我解决了问题,所以如果都失败了,我认为“纱线”值得一试。

EDIT:

编辑:

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

npm 过时时似乎有问题,更新它对我也有帮助。

回答by tksdotnet1

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://(ipaddress):(port)

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

  4. npm install -g @angular/cli

  1. Cd(将终端窗口路径更改为目标文件夹。

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

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

  4. npm install -g @angular/cli