DEV Community

Devtonight
Devtonight

Posted on • Updated on • Originally published at devtonight.com

How To Fix NPM Timeout Errors On Slow Internet Connections

Sometimes when we work with slow or unstable Internet connections, npm often fails to complete its commands like npm install and npm update with a timeout error. For that kind of situation, we can simply change some npm configurations like fetch-retries, fetch-retry-mintimeout, fetch-retry-maxtimeout and cache-min to minimize these timeout errors.

fetch-retries

This config controls the number of times npm try to connect to the registry when fetching packages. The default value is 2, but you can increase it to 3~5 or even more if you like. Open a terminal window and run the following command with the desired number of retries at the end.

npm config set fetch-retries 3
Enter fullscreen mode Exit fullscreen mode

fetch-retry-mintimeout

This config controls the minimum time (in milliseconds) npm wait before timing out when fetching packages from the registry. The default value is 10000 milliseconds (10 seconds), change it to 100000 or more.

npm config set fetch-retry-mintimeout 100000
Enter fullscreen mode Exit fullscreen mode

fetch-retry-maxtimeout

This config controls the maximum time (in milliseconds) npm wait before timing out when fetching packages from the registry. The default value is 10000 milliseconds (10 seconds), change it to 600000 or more.

npm config set fetch-retry-maxtimeout 600000
Enter fullscreen mode Exit fullscreen mode

cache-min

This config controls the minimum time (in seconds) to keep items before re-checking the registry. The default value is 10 seconds, change it to 3600 seconds (1 hour) or more.

npm config set cache-min 3600
Enter fullscreen mode Exit fullscreen mode

Finally, run this command to check whether all the configuration changes are successfully applied or not.

npm config ls -l
Enter fullscreen mode Exit fullscreen mode

Now try to run some failing commands to check whether they are working. The duration values mentioned in this question was only for demonstration, you may try different values by increasing and decreasing them according to your situation. Visit npm-config documentation for more available configurations.

Feel free to visit devtonight.com for more related content.

Top comments (1)

Collapse
 
juanluischaurant profile image
Juan Luis Chaurant

Testing the configuration right away!