WSL: npm commands are not working

My Windows build number is: Microsoft Windows [Version 10.0.17134.590]

I installed nodejs using this command: sudo apt install nodejs

When I run node -v I get: v8.10.0, so apparently, it’s properly installed, but when I run npm -v I get:

: not foundram Files/nodejs/npm: 3: /mnt/c/Program Files/nodejs/npm:
: not foundram Files/nodejs/npm: 5: /mnt/c/Program Files/nodejs/npm:
/mnt/c/Program Files/nodejs/npm: 6: /mnt/c/Program Files/nodejs/npm: Syntax error: word unexpected (expecting "in")

Also FYI, when I run which npm I get: /mnt/c/Program Files/nodejs/npm, but whne I run which node I get /usr/bin/node I have node installed on my windows to sometimes use it within Git bash. What can I do to solve this issue?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 44
  • Comments: 40

Most upvoted comments

$ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ # ... do node things
$ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ # ... do node things

this worked for me

I fixed my npm doing:

$ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin $ sudo apt install npm

You need restart visual studio code if you are using embed terminal.

install newest version of nodejs like below

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - sudo apt-get install -y nodejs

then you can use /usr/bin/npm instead of npm

the whereisnpm yields npm:

Then you don’t have npm installed.

$ sudo apt install npm

Which is like sudo apt install nodejs npm above only with one step instead of nodejs and npm separately.

Windows node is not same planet Linux node. Nothing is being installed again. The OP didn’t have npm installed on their Linux distro (usually Ubuntu) to begin with.

You can summarily ignore the unmount part way above; that was just in response to the “that didn’t work for me” message immediately above. [The “didn’t work” was just a case of the OP being lost, and unmounting was to guarantee no possibility of Windows node being picked up regardless of what “that didn’t work” was supposed to mean.]

The “actual solution” is to ensure you have all the node things installed on WSL, so they get picked up by the Linux node not Windows node. “Things” here means anything executable, notably shell scripts (including #!/usr/bin/env node scripts).

[The suggestion to launder the Windows components out of PATH is for people who have already polluted their paths beyond redemption. Noting that, if you don’t give your WSL user permission to pollute the Linux system paths, and you don’t give your Windows user permission to pollute C:\Program Files, you won’t have a conflict. It just gets folks who are going to play around briefly with node unblocked with a straightforward one-liner.]

How did this exactly work?

It took all the Windows paths out of $PATH, so you aren’t tripping over the node things you’ve installed on Windows.

$ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ # ... do node things

This worked for me. How did this exactly work? Sorry I am just a beginner.

Your $PATH still has /mnt/c/Program Files/nodejs (or whereis would not find that path otherwise). Which is why we’re setting the path to something sane.

image

This worked for me

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
. ~/.profile
sudo apt install npm
npm install my_node_packet

I had the same issue and was wondering if there is an actual solution for this. Does it make sense to install npm again? I thought the whole point would be to use the existing installation so it doesn’t make sense to unmount C, change PATH and do other npm install

It worked for me

Step 1. Inside WSL terminal

sudo nano /etc/wsl.conf

Add these lines:

[interop]
appendWindowsPath = false

Now close all the WSL terminals including those opened in VS Code.

Step 2. Open Windows PowerShell as Administrator

wsl --list
wslconfig /t Ubuntu-20.04 # your active version

Step 3. Open WSL

echo $PATH # it should include only linux paths

# check that node and npm do not mount to Windows paths
npm --version
# -bash: npm: command not found
node -v
# -bash: node: command not found

# now you can install node/npm inside WSL

p.s. this guide has helped me

Easiest solution for me was just doing alias npm /home/dragonwocky/n/bin/npm. Didn’t need to delete or replace or install anything. Just a nice and easy hey btw ubuntu this is where the right npm is.

All of the replies above seem to be installing npm on WSL. If you don’t want to install nodejs on WSL, and instead want to run the Windows counterpart, then these steps worked for me:

First of all note that since the Windows 10 Creators Update (build 1703, April 2017), we can natively run .exe files through WSL. You can try this by typing ipconfig.exe, or cmd.exe, and see both of them execute fine (given that you didn’t remove the Windows path that is automatically prepended) . If you have nodejs installed on Windows, you can even run node.exe, and it should work fine.

The problem is that all of these are .exe files. npm is an exception, as are all global node modules. If you go to /mnt/c/Program Files/nodejs, you will notice that npm is written as npm.cmd. You cannot run .cmd files from WSL as per my knowledge.

The way I go around this is by running:

cmd.exe /c npm.cmd in the current location. You should see output then.

You will notice that with your global node modules (inside /mnt/c/Users/<USERNAME>/AppData/Roaming/npm), you will not be able to call them from WSL either. Again, this is because they are .cmd instead of .exe.

We must create alias for these again if you want to use them through WSL. You can actually do this easily programmatically in your ~/.bashrc file:

Full solution:

cmd() {
  CMD=$1
  shift;
  ARGS=$@
  WIN_PWD=`wslpath -w "$(pwd)"`
  cmd.exe /c "pushd ${WIN_PWD} && ${CMD} ${ARGS}"
}

alias npm="cmd npm"

for file in `ls /mnt/c/Users/<USERNAME>/AppData/Roaming/npm/[a-z]*.cmd`; do
	cmdFile=$(basename -- "$file")
	filename="${cmdFile%.*}"
	alias "$filename"="cmd ${cmdFile}"
done

The reason we need the pushd command in cmd is because we need cmd to keep the same path as we had on WSL.

After restarting your terminal, you should be able to run npm --version and global commands like ng easily.

I fixed my npm doing:

$ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin $ sudo apt install npm

You need restart visual studio code if you are using embed terminal.

This works for me thanks a ton to help me out I just searched over n number of post to find the solution

Ran into this wsl npm headache today. If other solutions did not work for you, this is what I did. For this example, it is important to understand what the goal is for this case so you can use this example to show the error exists, do the fixes, and see if it fixed it for you as well.

$ cat broken.js
const puppeteer = require('puppeteer-core');

$ node broken.js
# Some errors related to "Cannot find module puppeteer-core"

$ npm i install  puppeteer-core
# Some errors, maybe something related to package.json 
# After a few hours, I begin to doubt my career choice.

1- Optional…UNINSTALL ubuntu wsl but don’t forget to Run history first and back up your files. 2- Go to the microsoft store and search for Linux. Choose the Microsoft provided Debian wsl. 3- sudo apt update 4- sudo apt upgrade 5- sudo apt install wget 6- wget https://nodejs.org/dist/v12.16.3/node-v12.16.3-linux-x64.tar.xz 7- Read this install guide https://github.com/nodejs/help/wiki/Installation#how-to-install-nodejs-via-binary-archive-on-linux 8 - sudo mkdir -p /usr/local/lib/nodejs 9- sudo tar -xJvf node-v12.16.3-linux-x64.tar.xz -C /usr/local/lib/nodejs might also need this for tar…sudo apt-get install xz-utils 10- Put this on the last line of your ~/.profile file

$ export PATH=/usr/local/lib/nodejs/node-v12.16.3-linux-x64/bin:$PATH

Run this to update your terminal

$ . ~/.profile

11- which node 12- which npm 13- sudo apt install dos2unix 14- dos2unix broken.js 15- npm i puppeteer-core 16- node broken.js

Twitter is the best way to reach me. I think those are all the steps, but ping me @mabrafoo if needed.

@therealkenc @bterlson Can Any one tell me how to undo this $ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin $ # ... do node things

Please Anyone tell me how to undo this i tried and how i’m having problems

There’s an alternative. Install yarn like this: curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt update && sudo apt install yarn I used Yarn installation documentation of Linux and I have tried and tested it. Replace && in command with ;; if you are using fish shell.

$ sudo umount /mnt/c # for good measure

In any event, I don’t know the steps you followed to get into the state (starting with installing nodejs on Windows). So I can’t give you specific steps to undo the state.

sudo apt install nodejs npm works here with a well-formed $PATH. If you don’t have npm installed on Ubuntu then you should have got npm not found. whereis npm should yield /usr/bin/npm. Bonne chance.