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
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 npmYou 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
Then you don’t have
npminstalled.Which is like
sudo apt install nodejs npmabove only with one step instead ofnodejsandnpmseparately.Windows
nodeis not same planet Linuxnode. Nothing is being installed again. The OP didn’t havenpminstalled on their Linux distro (usually Ubuntu) to begin with.You can summarily ignore the
unmountpart 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, andunmounting was to guarantee no possibility of Windowsnodebeing picked up regardless of what “that didn’t work” was supposed to mean.]The “actual solution” is to ensure you have all the
nodethings installed on WSL, so they get picked up by the Linuxnodenot Windowsnode. “Things” here means anything executable, notably shell scripts (including#!/usr/bin/env nodescripts).[The suggestion to launder the Windows components out of
PATHis 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 polluteC:\Program Files, you won’t have a conflict. It just gets folks who are going to play around briefly withnodeunblocked with a straightforward one-liner.]It took all the Windows paths out of
$PATH, so you aren’t tripping over the node things you’ve installed on Windows.This worked for me. How did this exactly work? Sorry I am just a beginner.
Your
$PATHstill 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.This worked for me
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
Add these lines:
Now close all the WSL terminals including those opened in VS Code.
Step 2. Open Windows PowerShell as Administrator
Step 3. Open 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
npmonWSL. If you don’t want to installnodejsonWSL, 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
.exefiles throughWSL. You can try this by typingipconfig.exe, orcmd.exe, and see both of them execute fine (given that you didn’t remove the Windows path that is automatically prepended) . If you havenodejsinstalled on Windows, you can even runnode.exe, and it should work fine.The problem is that all of these are
.exefiles.npmis an exception, as are all global node modules. If you go to/mnt/c/Program Files/nodejs, you will notice thatnpmis written asnpm.cmd. You cannot run.cmdfiles fromWSLas per my knowledge.The way I go around this is by running:
cmd.exe /c npm.cmdin the current location. You should see output then.You will notice that with your
globalnode modules (inside/mnt/c/Users/<USERNAME>/AppData/Roaming/npm), you will not be able to call them fromWSLeither. Again, this is because they are.cmdinstead 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~/.bashrcfile:Full solution:
The reason we need the
pushdcommand 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 --versionand global commands likengeasily.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.
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
Run this to update your terminal
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 thingsPlease 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 yarnI 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 measureIn 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 npmworks here with a well-formed$PATH. If you don’t havenpminstalled on Ubuntu then you should have gotnpmnot found.whereis npmshould yield/usr/bin/npm. Bonne chance.