[BUG] Npm opens many connections when installing
Problem
Is there an existing issue for this? - [X] I have searched the existing issues This issue exists in the latest npm version - [X] I am using the latest npm Current Behavior My project needs around 2000 packages. When running "npm install" it starts out by opening 2000 connections and then when all the connections are open it downloads 1 package on each connection. Is this intended?, because it causes a quite high load on our package proxy. Is there a way to limit the number of connections? With npm 9.9 it only uses 18/20 connections. Expected Behavior "npm install" only opens a few connetions to the package server. Steps To Reproduce 1. Clear cache 2. npm install or npm ci Environment - npm: 10.2.5 - Node.js: 18.19.0 - OS Name: Windows 11 - System Model Name: - npm config: [code block]
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Limit Concurrent Connections During Npm Install
The high number of concurrent connections during 'npm install' is due to npm's default behavior which attempts to maximize throughput by opening multiple connections to download packages simultaneously. This can overwhelm package proxies and lead to performance degradation, especially when dealing with a large number of packages.
Awaiting Verification
Be the first to verify this fix
- 1
Set Maximum Concurrent Connections
Modify the npm configuration to limit the number of concurrent connections. This can be done by setting the 'maxsockets' option, which controls the maximum number of concurrent connections to the registry.
bashnpm config set maxsockets 20 - 2
Clear Npm Cache
Before running the installation again, clear the npm cache to ensure that any previously cached connections do not affect the new settings.
bashnpm cache clean --force - 3
Run Npm Install
After configuring the maximum sockets and clearing the cache, run the 'npm install' command again to observe the changes in connection behavior.
bashnpm install - 4
Monitor Network Load
Use network monitoring tools to observe the number of connections being opened during the installation process. Ensure that the number of concurrent connections is now limited to the specified maximum.
bashUse tools like Wireshark or built-in OS network monitors.
Validation
Confirm that the number of concurrent connections during 'npm install' is limited to the configured maximum (e.g., 20). Check the network load on your package proxy to ensure it is manageable and does not exceed expected thresholds.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep