Skip to content

WSL Performance Issues with NPM/Yarn

Overview

Windows Subsystem for Linux (WSL) users often experience significant performance degradation when working with Node.js tools like NPM and Yarn compared to native Windows environments. This article explores the root causes and provides practical solutions to improve WSL performance for JavaScript development workflows.

Root Cause: Filesystem Performance

The primary cause of WSL performance issues is the filesystem architecture:

  • WSL2 uses a virtualized Linux kernel with a 9P protocol to access Windows drives
  • This protocol is significantly slower than native NTFS or Linux ext4 filesystems
  • Operations on Windows-mounted drives (/mnt/c/) can be 10-50x slower than native performance

WARNING

Always avoid storing project files on Windows-mounted drives (/mnt/c/, /mnt/d/, etc.) when working with WSL2. The 9P filesystem protocol creates substantial performance overhead for I/O-intensive operations like package installation.

Performance Comparison

Typical performance differences observed:

OperationNative WindowsWSL2 on /mnt/c/WSL2 on Linux FS
npx create-react-app10-15 seconds4-5 minutes15-30 seconds
git status<1 second60-120 seconds<1 second
npm installVaries by project5-10x slowerNear-native speed

1. Store Projects on WSL Native Filesystem

Move your project to the Linux filesystem (e.g., /home/username/projects/):

bash
# Copy project from Windows to WSL native filesystem
rsync -av /mnt/c/path/to/windows/project/ ~/projects/my-project --exclude node_modules

# Or clone directly to WSL filesystem
cd ~
git clone <repository-url>

TIP

Use rsync with --exclude node_modules to avoid copying already-installed dependencies, which can be reinstalled faster on the native filesystem.

2. Access WSL Files from Windows Applications

Use the \\wsl.localhost\ path to access WSL files from Windows applications:

\\wsl.localhost\Ubuntu\home\username\projects\my-project

This allows tools like VS Code to work with files directly on the WSL filesystem while maintaining performance.

3. Configure WSL to Start in Linux Filesystem

Add to your ~/.bashrc to automatically start in your project directory:

bash
echo 'cd ~/projects' >> ~/.bashrc

4. Alternative: Use WSL1 for Cross-Filesystem Work

If you must work with files on Windows drives, consider using WSL1:

bash
# Export current WSL2 distribution
wsl --export Ubuntu ubuntu_backup.tar

# Import as WSL1
wsl --import Ubuntu_WSL1 C:\WSL\Ubuntu_WSL1 ubuntu_backup.tar --version 1

# Set as default (optional)
wsl --set-default Ubuntu_WSL1

DANGER

WSL1 has compatibility limitations with some Linux software and hasn't been updated recently. Use only if cross-filesystem performance is absolutely necessary.

5. Network Performance Optimizations

For slow network operations (package downloads), enable Large Send Offload:

powershell
# Run as Administrator in PowerShell
Enable-NetAdapterLso -Name vEthernet* -IncludeHidden -IPv4
Enable-NetAdapterLso -Name vEthernet* -IncludeHidden -IPv6

6. Antivirus Exclusions

Add WSL and development tool exclusions to Windows Defender:

  1. Go to Windows Security → Virus & threat protection → Manage settings
  2. Under Exclusions, add folders:
    • %USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited*
    • Your project directories
  3. Add processes: node.exe, npm.cmd, yarn.cmd

7. NFS Alternative (Advanced)

For advanced users, set up an NFS server on Windows:

bash
# Install nfs-common in WSL
sudo apt-get install nfs-common

# Add to /etc/fstab
172.18.192.1:/c/Users/youruser/projects /home/youruser/projects nfs nfsvers=3,nolock 0 0

# Mount
sudo mount -a

File Watching Issues

The permission errors for system files occur because Node.js file watchers try to monitor entire directories, including protected system files. Solutions:

  1. Move project to WSL filesystem (prevents access to Windows system files)
  2. Configure watching tools to exclude system directories
  3. Use polling as fallback for projects on Windows drives

For Create React App, create a .env file with:

CHOKIDAR_USEPOLLING=true

Verification

Test performance improvements with:

bash
# Time a simple operation
time npm install

# Or use a simple script to test multiple operations
#!/bin/bash
echo "Testing filesystem performance..."
time ls -la > /dev/null
time find . -name "*.js" | wc -l > /dev/null

When to Reinstall WSL

If performance degrades over time, consider exporting your environment and reinstalling:

bash
# Export current installation
wsl --export Ubuntu ubuntu_backup.tar

# Unregister and reimport
wsl --unregister Ubuntu
wsl --import Ubuntu . ubuntu_backup.tar

Conclusion

WSL2 performance issues with NPM/Yarn primarily stem from filesystem architecture limitations. The most effective solution is storing projects on the native Linux filesystem and using appropriate methods to access them from Windows applications. For edge cases where Windows filesystem access is unavoidable, consider WSL1 or advanced solutions like NFS mounting.

INFO

Microsoft continues to improve WSL2 performance. Check the WSL GitHub repository for updates on 9P filesystem improvements and other enhancements.