skip to Main Content

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

enter image description here

will sit like this for 6 hours, then continue or Ctrl C out, then:

enter image description here

run again and it works.

Am a beginner at node and npm.

3

Answers


  1. 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:

    1. Clear npm and Node cache:
      Sometimes, cache inconsistencies can cause npm to stall or fail. Since you’re already using npm cache clean --force, this step is covered, but you might also want to clear the cache for nvm (Node Version Manager) to rule out further cache issues.

      nvm cache clear
      
    2. 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:

      sudo chown -R $(whoami) ~/.npm
      sudo chown -R $(whoami) ~/.config
      
    3. 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:

      npm set fetch-retry-mintimeout 20000
      npm set fetch-retry-maxtimeout 120000
      npm set fetch-retry-factor 10
      
    4. 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.

      npm set prefer-online false
      npm set use-ipv4 true
      
    5. 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:

      sudo nano /etc/resolv.conf
      

      Add the following lines:

      nameserver 8.8.8.8
      nameserver 8.8.4.4
      
    6. 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.

      npm set registry https://registry.npmjs.org/
      
    7. Verify nvm and Node Versions:
      Occasionally, certain versions of npm and node have bugs that cause unexpected behavior. You might want to try a stable LTS version of Node.js, such as 18.x or 20.x, instead of 22.11.0.

      nvm install 20
      nvm use 20
      
    8. 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.

    9. 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:

      wsl --update
      
    10. Enable Verbose Logging for More Details:
      To get more insight into what is happening during the stalling, you can enable even more verbose logging:

      npm install --loglevel=silly
      

    Following these steps should help narrow down the problem.

    Login or Signup to reply.
  2. 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:

    1.  Run npm cache clean --force.
    2.  Run npm install --verbose.
    

    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.

    Login or Signup to reply.
  3. Please run npm config command and see if TMP variable is set to a single path.

    npm config ls -l 
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search