WSL: Node.js and NPM network dramatically slow

Windows Build Number

Windows 11 Pro 21H2 Build: 22000.100

WSL Version

  • WSL 2
  • WSL 1

Kernel Version

Kernel version: 5.10.43

Distro Version

Ubuntu 20.04

Other Software

I have Ubuntu, Docker (v20.10.7), WSL 2 installed in my system. I am using nvm with node.js (tried different versions still the same results)

Repro Steps

Cloned my fresh repo. npm install - might pass and actually install. Whenever I want to install an additional package - it might take forever to install.

Expected Behavior

Should run smoothly, without a long time-consuming.

Actual Behavior

I have dramatically slow network speed while working with Node.js and NPM.

  • Request made from Node.js app - randomly are slow, unstable, timeouts
  • npm install might take forever

I have to cancel and run npm install again, and if I am lucky - all packages will be installed, otherwise, I have to cancel and run again. I am unsure why cache revalidation takes so much time image

Diagnostic Logs

I ran npm install --verbose Here 24 mins duration highlighted, so I just canceled. image

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 29
  • Comments: 50 (1 by maintainers)

Most upvoted comments

@ssalbdivad Hi, Please try the following in your WSL

sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
sudo chattr +i /etc/resolv.conf

Then reboot and check if it works for you 😃 For me, it looks like solved. Did 4 reboots - works as expected.

Reopening this to track this issue.

I think that’s a workaround but not a solution. because out-of-the-box WSL doesn’t work as expected and requires manual manipulations.

IPv6 was also the root cause for me as well. This answer resolved it for me without having to fully disable IPv6: https://askubuntu.com/a/1200257

Instead of disabling IPv6 completely, you can instruct Ubuntu to prefer IPv4 connections to IPv6 by editing the precedence blocks in /etc/gai.conf.

Uncomment the last lines. Check that file has the next uncommented lines.

#For sites which prefer IPv4 connections change the last line to
precedence ::ffff:0:0/96 100
...
#    For sites which use site-local IPv4 addresses behind NAT there is
#    the problem that even if IPv4 addresses are preferred they do not
#    have the same scope and are therefore not sorted first.  To change
#    this use only these rules:
#
scopev4 ::ffff:169.254.0.0/112  2
scopev4 ::ffff:127.0.0.0/104    2
scopev4 ::ffff:0.0.0.0/96       14

These were the last 4 lines in the file.

Closing, due to solution.

@ssalbdivad Hi, Please try the following in your WSL

sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
sudo chattr +i /etc/resolv.conf

Then reboot and check if it works for you 😃 For me, it looks like solved. Did 4 reboots - works as expected.

Same problems. Win 11

Node is unusable because of this, TBH

I’m having the same issue as of today, really bizarre 🤷

I have also run into that issue and I’m still on Win 10 with WSL2 on kernel version 5.10.16. The workaround with resolv.conf brakes all DNS resolving so this is not a solution.

If you somehow fix it, please reply in this thread. It really annoying issue…

Will do. I’m back on native Ubuntu for now since I can’t reliably update my project’s dependencies.

Would be great if someone on the WSL team could take a look given that whatever set of circumstances causes this to happen effectively blocks node development on WSL, which I assume is one of its most popular applications.

I’m having the same issue as of today, really bizarre 🤷

If you somehow fix it, please reply in this thread. It really annoying issue…

In my case I did these, for an Existing project

  • Enable/Disable ipV6
  • remove package-lock.json and try again

The removal of package-lock worked like charm. 😃

Disabling IPv6 in the network adapter fixed instantly

Happened to me today on latest kernel. Luckily, the resolve.conf hack helped to resolve this issue.

I’m disappointed spending $2k+ on a Windows laptop instead of Mac.

PS C:\Users\Bohdan> wsl --update
Checking for updates...
No updates are available.
Kernel version: 5.10.60.1

Yeah I fixed the problem with wsl/node similarly but forgot to post here, sorry. My node was already under wsl but my projects were on the windows part. That’s why I had lags. Moved everything to wsl and lags vanished. Very strange though that this wsl-windows communication is so slow. I guess other lag issues might be due to hidden calls to windows part

FWIW I’ve experienced this issue several times and had given up on it.

I tried both changing /etc/resolv.conf and using http instead of https as described here.

What worked for me was installing node inside WSL using n and adding the following to my ~/.bashrc file (within WSL)

export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH

(note the order of the path where I place $N_PREFIX/bin before the path)

I had noticed that which npm was pointing to /mnt/c/Program Files/nodejs/npm... which led me to think it was running something on the windows side instead, which I assume adds some overhead when inside of WSL.

What I would add to the conversation here is: What do you get when you run which npm at the WSL bash prompt? If you get something like /mnt/c/Program Files/nodejs/npm then try what I’ve described here.

Still facing the same issue, MTU set to 1500 and tried the previous solution to delete the resolv.conf

It seem to have been an issue with too low MTU on my end.

List your interfaces from Windows:

netsh interface ipv4 show subinterfaces

Find the interface WSL is using, for me this was "vEthernet (WSL)", and my MTU defaulted to 576. (if it’s higher for you it might be a different issue)

Set the MTU on the interface to match your main interface, for me it was 1500:

netsh interface ipv4 set subinterface "vEthernet (WSL)" mtu=1500 store=persistent

(and maybe do a wsl --shutdown?)

Interesting, rollback brought me all the way back to 4.19.104, but I’m still having the same issue 🤨