Have you tried to run or install npm on WSL? This will conflict with the version on Windows, unless you’re careful. In this article, I will show you how to install node package manager (npm) on Windows Subsystem for Linux.
With Windows Subsystem for Linux (WSL), users can now use both Linux & Windows simultaneously. For users that are newer to operating both systems together, this can lead to some frustrations, as the systems sometimes conflict with eachother. One example, is node package manager, npm.
Why is NPM not working in WSL?
Linux and Windows both operate their file systems differently.
Windows stores most application files in C:\Program Files\Application. Windows is not case sensitive, file and FILE can exist in the same folder. Linux, splits its files between multiple locations – binaries in /usr/bin, libraries in /usr/lib, and config files in /etc/. Linux is case sensitive, and would not allow file and FILE to exist simultaneously.
Challenges arise when components from both systems mix, and environment variables from both systems pollute your PATH. To prevent conflicts, don’t give WSL user permission to pollute the Linux system paths, and don’t give Windows user permission to pollute C:\Program Files.
If this is the case, there is a quick fix that will solve your issues. This should not be considered a first step. If you’re installing WSL for the first time, skip the next step.
A quick fix - not a first step
If your environment variables are a complete mess, there is a solution that launders Windows components out of $PATH. Note that this should not be considered a first step, it’s just a quick one-liner for people who have polluted their Paths beyond redemption.
The solution is to remove all Windows components from PATH, which unblocks Node and npm.
Open up a new terminal, and type:
$ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Hit enter, and then restart terminal. This should free you up to use Node on WSL.
How to install npm on WSL
If your PATH is not a mess, or if you are installing WSL for the first time, follow these instructions to install npm on WSL.
1. Install curl
$ sudo apt-get install curl
2. Install nvm
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
Be sure to change the
v0.38.0 in the path to the most recent version available. Installing a newer version of nvm will replace the older one.
3. Restart terminal
4. Verify installation (returns nvm)
$ command -v nvm
5. Install Node.js (I recommend the stable one, out of the two choices.
Latest stable LTS release (recommended):
$ nvm install --lts
$ nvm install node
6. Verify installation
$ node --version
$ npm --version
When you install npm using nvm, and then install node on WSL, you won’t have to mess with your PATH. Doing so can get very messy, so try to keep it clean!