6

I'm using npm version 8.1.2 inside Ubuntu 20.04 in windows subsystem for linux 2 (WSL2).

I've been trying to install 5 dependencies for my project, yet the installation hangs.

npm install solc web3 mocha ganache-cli @truffle/hdwallet-provider --verbose

Yet, installation is taking forever (over 10 hours).

I already checked the following:

  1. npm version is up to date, I am using npm version 8.1.2
  2. I already updated the registry to use http instead of https using:
npm config set registry http://registry.npmjs.org/
  1. Checked internet connection which is running fine at 144 Mbps
  2. Tried the same installation in a different linux machine, which ran at the expected speed

Has anyone encountered this issue before? What has been the solution?

PS. There are indeed many topics regarding speed of npm install in stackoverflow, yet, I have already read a lot of them, which led me to testing the 4 topics mentioned before.

João A. Veiga
  • 498
  • 3
  • 11
  • 1
    Have you found a solution? I am experiencing the same issue right now. – Bo Fjord Jensen Jan 19 '22 at 07:43
  • I haven't found a solution, I have found an explanation though. Apparently, WSL2's filesystem integration with windows makes navigating folders extremally slow, which is why the entire npm install process takes forever. I resorted to developing on the windows environment for the time being, but it would also be possible to npm install on windows and just move the files to the WSL2 OS... – João A. Veiga Jan 19 '22 at 14:26
  • Yeah but that is only for accessing Windows files mounted in the `/mnt` directory, in my case I had my files all located in `/home` – Bo Fjord Jensen Jan 20 '22 at 07:41
  • have you tried installing during a different time? – Someone Special Jan 21 '22 at 03:12

3 Answers3

5

Assuming you are not working on files mounted in /mnt then the following worked for me. As a quick test I did wget -O - https://registry.npmjs.org and it was pretty obvious that name resolution was somehow the culprit.

I had the same issue and solved it by creating the /etc/wsl.conf file adding the following:

[network]
generateResolvConf = false

Then I replaced the existing /etc/resolv.conf symlink so you will have to remove it and then recreate it using your favourite text editor adding the following:

nameserver 8.8.8.8

Replace 8.8.8.8 with whatever IP address your DNS server of choice has, if you don't want to use Googles.

When done restart your WSL instance by running wsl --shutdown in an elevated Windows command prompt, then you can start your WSL instance like you normally would.

I don't know why the name resolution is so slow using the stock resolv.conf configuration, but this workaround makes it normal again.

Bo Fjord Jensen
  • 769
  • 5
  • 8
4

This are the correct steps that works for me:

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

Here is the link to the github issue:

https://github.com/microsoft/WSL/issues/7254#issuecomment-905767204
0

As mentioned in other comments, this happens to be a performance issue when working on a mounted folder of Windows within Unix using WSL2 (/mnt/c/whatever folder for example).

In my case, I ended up working on the unix system (i.e.: ~/projects/whatever or any other path you feel comfortable) with all the performance available (also you can open VS Code from windows, just type code . from your project folder). And then, if needed, replicating the changes into the shared folder, which will be faster as well, using rsync for example:

rsync -av ~/projects/whatever /mnt/c/whatever

Also you can exclude 'node_modules' if you don't need it on the destination:

rsync -av --exclude='node_modules' ~/projects/whatever /mnt/c/whatever

Hope this help!