Interesting problem with npm stalling, then running again the second time on dev Windows/WSL2 Ubuntu machine.
Windows 10, WSL2 running Ubuntu 22.04 LTS. No proxy or additional firewalls on network.
- nvm: 0.40.1
- Node: 22.11.0
- npm: 10.9.0
package.json (part of it)
"devDependencies": {
"react-email": "3.0.2"
}
then install
# will reliably fail each time if I do this
npm cache clean --force
# will fail the first time
# then work if I run it again
npm install --verbose
will sit like this for 6 hours, then continue or Ctrl C out, then:
run again and it works.
Am a beginner at node and npm.
3
Answers
The issue appears to stem from a few common causes when using
npm
within a Windows/WSL2 environment, especially with Ubuntu. Here’s a step-by-step troubleshooting guide tailored to this setup:Clear npm and Node cache:
Sometimes, cache inconsistencies can cause
npm
to stall or fail. Since you’re already usingnpm cache clean --force
, this step is covered, but you might also want to clear the cache fornvm
(Node Version Manager) to rule out further cache issues.Ensure Consistent Permissions:
There might be permission issues with WSL2 and
npm
that cause delays. Make sure your~/.npm
and~/.config
directories have the correct permissions:Set the Network Configuration for npm:
Sometimes, slow network configurations within WSL2 affect
npm
‘s ability to fetch packages. You can try configuring a different registry timeout to see if it mitigates the stalling issue:Disable IPv6 in npm (if applicable):
In some cases, npm may default to using IPv6, which can cause issues in certain network setups. Disabling it might improve connectivity and prevent stalls.
Check DNS Configuration:
WSL2 may have issues with DNS resolution that affect
npm
installs. You can update your DNS settings to use a reliable DNS service (like Google DNS or Cloudflare DNS) by modifying your/etc/resolv.conf
file:Add the following lines:
Use a Different npm Mirror Temporarily:
Changing the registry to a mirror can help in case there are specific connectivity issues with the default
npm
registry.Verify nvm and Node Versions:
Occasionally, certain versions of
npm
andnode
have bugs that cause unexpected behavior. You might want to try a stable LTS version of Node.js, such as18.x
or20.x
, instead of22.11.0
.Run
npm install
Twice as a Temporary Solution:As you’ve noticed, the install sometimes completes after running
npm install
a second time. This workaround might suffice temporarily if you are in a rush, but it’s better to find the root cause by following the steps above.Check WSL2’s Network Configuration:
WSL2 has known networking issues in some versions of Windows 10. Ensure you’re running the latest version of Windows with all updates applied, and consider updating your WSL2 installation:
Enable Verbose Logging for More Details:
To get more insight into what is happening during the stalling, you can enable even more verbose logging:
Following these steps should help narrow down the problem.
Here’s a properly formatted version of your question to post on Stack Overflow:
Title: npm stalls on first run, then works on second run in WSL2 Ubuntu on Windows 10
Details:
I’m encountering an issue on my development setup using Windows 10 with WSL2 running Ubuntu 22.04 LTS. Whenever I run npm install, the installation fails the first time, but it works when I run it again. The issue occurs consistently, and I’m not sure how to resolve it. There are no proxies or additional firewalls on the network.
Here are my versions:
• nvm: 0.40.1
• Node: 22.11.0
• npm: 10.9.0
package.json (part of it):
"devDependencies": {
"react-email": "3.0.2"
}
Steps to reproduce the issue:
The installation will fail the first time and just hang for a long time (up to 6 hours). I then stop the process with Ctrl+C and run npm install again, and it works fine. Here’s the verbose output after it starts working.
I’m new to Node and npm, so I’m unsure of what’s causing this. Any advice or suggestions would be greatly appreciated!
What I Tried:
• Running npm cache clean –force.
• Running npm install –verbose multiple times.
• Reinstalling npm and Node.js.
• Checking network configuration in WSL2 and Windows.
What I Expect:
A solution to ensure npm install works reliably on the first attempt, without needing to run it multiple times.
You can now copy and paste this directly into Stack Overflow. Be sure to include any relevant screenshots or logs where indicated.
Please run npm config command and see if TMP variable is set to a single path.
Check the values of TMP, TMPDIR and TEMP environment variables and ensure they are set to a single path.
This may be related to the issue discussed in this thread.