Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm commands are not working #3882

Closed
MuhammadJamaluddin opened this issue Feb 28, 2019 · 40 comments
Closed

npm commands are not working #3882

MuhammadJamaluddin opened this issue Feb 28, 2019 · 40 comments

Comments

@MuhammadJamaluddin
Copy link

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?

@therealkenc
Copy link
Collaborator

Same as #1512 which is Variation #2365 (message) etc. Root cause #1896 ref some insight in #3855 (message).

@MuhammadJamaluddin
Copy link
Author

Thanks for replying. I've skimmed through them, but couldn't really catch a solution & I'm afraid to mess with my current environment. Could you please guide me though one?

@therealkenc
Copy link
Collaborator

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

@MuhammadJamaluddin
Copy link
Author

That didn't work for me.

@therealkenc
Copy link
Collaborator

therealkenc commented Feb 28, 2019

$ 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.

@MuhammadJamaluddin
Copy link
Author

whereisnpm yields npm: /mnt/c/Program Files/nodejs/npm /mnt/c/Program Files/nodejs/npm.cmd

@therealkenc
Copy link
Collaborator

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

@MuhammadJamaluddin
Copy link
Author

MuhammadJamaluddin commented Feb 28, 2019

When I run the path command, the whereisnpm yields npm: & when I restart the bash & run whereisnpm again, it's the same as before...

@therealkenc
Copy link
Collaborator

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.

@rainyman2012
Copy link

rainyman2012 commented Jun 26, 2019

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

@kigold
Copy link

kigold commented Aug 9, 2019

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

this worked for me

@arthursoas
Copy link

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.

@itsrhed
Copy link

itsrhed commented Oct 18, 2019

$ 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.

@therealkenc
Copy link
Collaborator

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.

@coder-examples
Copy link

coder-examples commented Oct 22, 2019

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.

@ftorrado
Copy link

ftorrado commented Dec 5, 2019

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

@therealkenc
Copy link
Collaborator

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.]

@seancanning37
Copy link

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

exactly what i was looking for! thanks!
Added this to my .bashrc and now i can run npm from the Ubuntu LTS from windows

@ozzy1981
Copy link

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

this worked for me

This also worked for me

@darkRaspberry
Copy link

darkRaspberry commented Apr 22, 2020

@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

@Compiler-Error
Copy link

Compiler-Error commented May 2, 2020

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.

@Karlheinzniebuhr
Copy link

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

@richkwart
Copy link

Thanks @Karlheinzniebuhr your solution worked for me

@luciavelasco
Copy link

I had this issue out of the blue after using npm with no problems on WSL for several months. Bizarrely, it fixed itself after a couple of hours with no intervention from me. Suggested alternative fix: wait and pray to the WSL gods? 😕

@dragonwocky
Copy link

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.

@jonathonnordquist
Copy link

jonathonnordquist commented Jul 21, 2020

I was running Node 12 on WSL using NVM and run into the same issue as OP when trying to install Mongoose. I ran the "magic" command above export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin and the situation only went from bad to worse. Now when running npm, nvm or node the applications are not found. I don't know what kind of strangeness this command introduced into my system but in my experience this kind of failure cascade often leads to a complete reinstall of the development environment. To get up and running again I had to remove the .nvm and .npm directories, remove the path from my .bash_profile and do a complete reinstall of NVM and Node. I would council anyone against using this export call in WSL for this issue unless they are 100% certain it will correct the problem.

@therealkenc
Copy link
Collaborator

therealkenc commented Jul 22, 2020

but in my experience this kind of failure cascade often leads to a complete reinstall of the development environment

You can entirely reverse the export by not doing it. That line doesn't even survive a new shell instance (unless you've put it in your .bashrc) let alone require a "complete reinstall of the development environment". The reason that export temporary hack (to eliminate the original poster's Windows node from the equation) didn't work for you is because you installed node with nvm, and that minimal $PATH doesn't include your $HOME/.nvm. That isn't how node was installed in the OP. That person installed node via sudo apt install nodejs and he was missing the commensurate sudo apt install npm. [Or one liner: sudo apt install nodejs npm.]

And to be clear: That export isn't actually suggested as a "fix" for anything because there isn't anything broken (in the context). This issue is a dupe of linux-behavior shell scripts that contain CRLF. Another (near equivalent) approach would be to forgo interop and use wsl.conf appendWindowsPath=false. [And that's only to avoid getting your Windows and WSL node installs crossed. Which is unnecessary once a user is familiar enough to not get themselves in a state that is crossed.]

The guidance to anyone landing in an npm related issue in this github is to back away slowly. You want a NodeJS forum. The majority of people who land in this issue (and the half-dozen others) are people unfamiliar with node and npm, shell CRLF vagaries, paths, and the rest. As a result the advice being offered is near uniformly bad. Then, once a person becomes familiar with how node and npm and nvm and the paths work (and notably the difference between installing a package locally and globally, and the difference between installing something in Windows and WSL) they dissapear never to be seen again. Because why would anyone familiar with node and npm hang out in a WSL bugtracker.

I speak up because a lot of people struggling with node on WSL are new and they are being led astray. And it is hard to watch.

There is some guidance on setting up node on WSL here (noting, the steps aren't actually related to or in any way unique to WSL). It happens to use the nvm approach... and that's not bad in any way, but isn't what the OP in this thread was doing. Bonne chance.

@cnell7
Copy link

cnell7 commented Sep 4, 2020

Thank you so much man. You saved me a headache!

@amin-dashti
Copy link

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

it`s work me !
thank you

@djamaile
Copy link

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

This worked for me.

@undefined-dev74
Copy link

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

@Abdullah-Sajjad026
Copy link

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

this worked for me

worked for me too. Thanks!

@dharm1k987
Copy link

dharm1k987 commented Jan 9, 2021

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.

@yavuz-bektas
Copy link

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

worked for me also! thanks.

@nikhilvslogicplum
Copy link

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.

Thank you for the answer ! It really helped me.

@SrLeet03
Copy link

sudo apt install nodejs npm
This worked man, Thenks! I was unware of installing npm separately on my machine.

@paulrogov
Copy link

paulrogov commented Feb 4, 2022

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

@hanzlahabib
Copy link

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

This Worked for me

Thanks

@hanzlahabib
Copy link

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

This Worked for me

Thanks

One side effect which I had after doing this, is now I cannot directly run code. command to open my project in vscode from WSL terminal, a quick solution is to open vscode and from there open WSL window and then navigate to the desired folder,
if you have suggestions for this, please let us know; thanks

@hanzlahabib
Copy link

hanzlahabib commented Aug 17, 2022

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

This Worked for me
Thanks

One side effect which I had after doing this, is now I cannot directly run code. command to open my project in vscode from WSL terminal, a quick solution is to open vscode and from there open WSL window and then navigate to the desired folder, if you have suggestions for this, please let us know; thanks

Update:
FROM THE WINDOWS COMMAND PROMPT
To open a WSL window directly from a Windows prompt use the --remote command line parameter:

code --remote wsl+distro name 'path in WSL'

https://code.visualstudio.com/docs/remote/wsl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests