Yarn: There appears to be trouble with your network connection. Retrying

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. Yarn: There appears to be trouble with your network connection. Retrying
  2. Delete your node_modules folder and yarn.lock file
  3. Make sure your proxy is configured correctly
  4. Turn real-time protection off temporarily

# Yarn: There appears to be trouble with your network connection. Retrying

The yarn issue "There appears to be trouble with your network connection. Retrying..." occurs when your internet connection is too slow to install the specified package.

To solve the error, set the --network-timeout flag to increase the timeout when running the yarn add command.

shell
yarn add <YOUR_PACKAGE> --network-timeout 300000

yarn install package with network timeout flag

Make sure to replace the <YOUR_PACKAGE> placeholder with the actual name of the package you're trying to install.

The --network-timeout flag can be used to set the timeout of the installation command to N milliseconds.

If your command times out when installing all modules in your project, simply set the --network-timeout flag without specifying the name of a package.

shell
yarn --network-timeout 300000

yarn install all modules with network timeout flag

You can also set the network-timeout flag globally for all yarn commands so you don't have to remember to set it every time.

shell
yarn config set network-timeout 500000 -g

yarn config set network timeout flag globally

The next time you run the yarn command to install your modules or yarn add to install a specific module, the network-timeout flag will be set to 500 seconds.

Increasing the timeout is often necessary when installing large packages or when your network connection is too slow.

# Delete your node_modules folder and yarn.lock file

If the issue persists, try to delete your node_modules folder and yarn.lock file and reinstall your dependencies.

If you are on Windows, run the following commands in CMD (Command Prompt).

cmd
# Windows rd /s /q "node_modules" del -f yarn.lock del package-lock.json # 👇️ clean your yarn cache yarn cache clean yarn

If you are on macOS or Linux, run the following commands in bash or zsh.

shell
# macOS and Linux rm -rf node_modules rm -f yarn.lock rm -f package-lock.json # 👇️ clean your yarn cache yarn cache clean yarn

Deleting your yarn.lock file and reinstalling your dependencies often resolves the issue.

# Make sure your proxy is configured correctly

If you are trying to install a package from behind a proxy, make sure it is configured correctly.

For example, if you want to remove the proxy, run the following commands.

shell
npm config rm http-proxy npm config rm https-proxy yarn config delete proxy yarn config delete https-proxy

Try to run your yarn installation command after removing the proxy setting.

shell
# Installing a specific package yarn add <YOUR_PACKAGE> --network-timeout 300000 # Installing all packages yarn --network-timeout 300000

If you instead want to set a proxy, run the following commands.

shell
yarn config set proxy http://your_proxy_url:port yarn config set https-proxy https://proxy.company:8080

If you need to specify your credentials (username and password), use the following commands instead.

shell
yarn config set proxy http://USERNAME:PASSWORD@your_proxy_url:port yarn config set https-proxy https://USERNAME:PASSWORD@proxy.company:8080

You can use the following command to verify that your proxy settings are set correctly.

shell
yarn config get proxy yarn config get https-proxy

# Turn real-time protection off temporarily

If you are on Windows and the issue persists, try to turn off real-time protection temporarily:

  1. Click on Start 🪟.
  2. Select Settings > Update & Security > Windows Security > Virus & threat protection > Manage settings. (In early versions of Windows 10, select Virus & threat protection > Virus & threat protection settings instead).
  3. Switch the Real-time protection setting to Off and click on Yes to confirm.

Try to run your yarn installation command after disabling real-time protection.

shell
# Installing a specific package yarn add <YOUR_PACKAGE> --network-timeout 300000 # installing all packages yarn --network-timeout 300000

If the issue persists, check out the following article:

You might have set the http_proxy or https_proxy environment variables.

You can unset them by following the steps in this subheading.

On macOS and Linux, you can use the following commands to unset the environment variables.

shell
# on macOS and Linux unset http_proxy unset https_proxy unset HTTP_PROXY unset HTTPS_PROXY

On Windows, in CMD, use the following commands instead.

shell
# on Windows (CMD) set http_proxy=null set https_proxy=null set HTTP_PROXY=null set HTTPS_PROXY=null

You can also try to set your NPM registry to npmjs.org.

shell
npm config set registry http://registry.npmjs.org/

Try to run your yarn installation command after making the change.

shell
# Installing a specific package yarn add <YOUR_PACKAGE> --network-timeout 300000 # Installing all packages yarn --network-timeout 300000

If that didn't work, try to set your NPM registry using HTTPS.

shell
npm config set registry https://registry.npmjs.org/

Try to rerun your yarn installation command after.

shell
# Installing a specific package yarn add <YOUR_PACKAGE> --network-timeout 300000 # Installing all packages yarn --network-timeout 300000

If the issue persists, try to install the long-term supported version of Node.js by following the instructions in this subheading.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.