Introduction
In this article, we will discuss how to fix the "Node NPM Command Not Found" error in WSL2 (Windows Subsystem for Linux 2). This error occurs when you try to run Node.js or NPM commands in the WSL terminal, but the system cannot locate the Node.js installation. We will cover the key concepts, provide context, and detailed steps to resolve this issue.
Background
Node.js is a popular JavaScript runtime environment, and NPM (Node Package Manager) is the default package manager for Node.js. WSL2 is a compatibility layer for running Linux binary executables natively on Windows 10. However, sometimes the Node.js installation may not be detected correctly in the WSL2 terminal.
Symptoms
The primary symptom of this issue is the "Node NPM Command Not Found" error message when you try to run Node.js or NPM commands in the WSL2 terminal. For example:
$ node -v
node: command not found
Or:
$ npm install
npm: command not found
Causes
There are several reasons why you might encounter this issue:
- Node.js is not installed or not installed correctly in WSL2.
- The Node.js installation path is not added to the WSL2 PATH environment variable.
- There is a conflict between the Node.js installations in different environments (Windows, WSL2, or both).
Solution
Step 1: Install Node.js in WSL2
If you haven't installed Node.js in WSL2 yet, follow these steps:
- Open the WSL terminal and update the package index:
- Install Node.js and NPM:
$ sudo apt update
$ sudo apt install nodejs npm
Step 2: Verify Node.js Installation
After installing Node.js, verify the installation by checking the Node.js version:
$ node -v
The output should be the version number of Node.js installed:
v14.17.3
Step 3: Add Node.js to PATH
If Node.js is installed but not detected in the WSL2 terminal, add the Node.js installation path to the PATH environment variable:
- Open the WSL terminal and edit the .bashrc file:
- Add the following line at the end of the file:
- Save and close the file:
- Reload the terminal:
$ sudo nano ~/.bashrc
export PATH=$PATH:/usr/bin/node
$ sudo nano --save ~/.bashrc
$ source ~/.bashrc
Step 4: Check for Conflicts
If there is a conflict between Node.js installations in different environments, uninstall Node.js from Windows and use the WSL2 installation instead:
$ sudo apt remove nodejs npm
Then, follow the steps above to install Node.js in WSL2.
Summary
In this article, we discussed how to fix the "Node NPM Command Not Found" error in WSL2 by installing Node.js in WSL2, adding it to the PATH environment variable, and checking for conflicts. By following these steps, you should be able to run Node.js and NPM commands in the WSL2 terminal without any issues.