WSL icon indicating copy to clipboard operation
WSL copied to clipboard

npm commands are not working

Open MuhammadJamaluddin opened this issue 7 years ago • 40 comments

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?

MuhammadJamaluddin avatar Feb 28 '19 00:02 MuhammadJamaluddin

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

therealkenc avatar Feb 28 '19 02:02 therealkenc

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?

MuhammadJamaluddin avatar Feb 28 '19 05:02 MuhammadJamaluddin

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

therealkenc avatar Feb 28 '19 05:02 therealkenc

That didn't work for me.

MuhammadJamaluddin avatar Feb 28 '19 05:02 MuhammadJamaluddin

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

therealkenc avatar Feb 28 '19 05:02 therealkenc

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

MuhammadJamaluddin avatar Feb 28 '19 05:02 MuhammadJamaluddin

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

therealkenc avatar Feb 28 '19 06:02 therealkenc

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

MuhammadJamaluddin avatar Feb 28 '19 06:02 MuhammadJamaluddin

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.

therealkenc avatar Feb 28 '19 06:02 therealkenc

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

rainyman2012 avatar Jun 26 '19 22:06 rainyman2012

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

this worked for me

kigold avatar Aug 09 '19 08:08 kigold

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.

arthursoas avatar Oct 01 '19 12:10 arthursoas

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

itsrhed avatar Oct 18 '19 11:10 itsrhed

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.

therealkenc avatar Oct 18 '19 16:10 therealkenc

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.

shahaayush265 avatar Oct 22 '19 10:10 shahaayush265

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

ftorrado avatar Dec 05 '19 11:12 ftorrado

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

therealkenc avatar Dec 05 '19 18:12 therealkenc

$ 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

seancanning37 avatar Apr 04 '20 18:04 seancanning37

$ 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

ozzy1981 avatar Apr 22 '20 05:04 ozzy1981

@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

darkRaspberry avatar Apr 22 '20 10:04 darkRaspberry