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

WSL prefers NodeJS executable in Program Files over /usr/bin #1896

Closed
Entroper opened this issue Apr 12, 2017 · 20 comments
Closed

WSL prefers NodeJS executable in Program Files over /usr/bin #1896

Entroper opened this issue Apr 12, 2017 · 20 comments

Comments

@Entroper
Copy link

Entroper commented Apr 12, 2017

I've just tried installing NodeJS on WSL, after running the Creators Update and doing a fresh WSL install to get 16.04.

https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

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

Running node works fine:

$ node -v
v6.10.2

But npm doesn't work:

$ npm -v
: 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")

It seems to be trying to run the npm executable from Windows. I tried some other things:

$ whereis npm
npm: /usr/bin/npm /mnt/c/Program Files/nodejs/npm /mnt/c/Program Files/nodejs/npm.cmd /mnt/c/Users/Entroper/AppData/Roaming/npm/npm /mnt/c/Users/Entroper/AppData/Roaming/npm/npm.cmd
$ which npm
/usr/bin/npm
$ /usr/bin/npm -v
3.10.10
$ whereis node
node: /usr/bin/node /usr/include/node /mnt/c/Program Files/nodejs/node.exe /usr/share/man/man1/node.1.gz
$ which node
/usr/bin/node

So it obviously knows where the Linux npm is, and it seems to think that's the preferred one, but it doesn't run that one for some reason. And it clearly works if I specify the absolute path to npm.

@benhillis
Copy link
Member

benhillis commented Apr 12, 2017

I've set this up locally by installing the Windows and Linux versions of nodejs and I'm not hitting the same issue as you. My shell is correctly picking up the npm in /usr/bin and not the one in Program Files.

Could you share the output of the below commands?
echo $PATH
strace -v npm -v

It looks like you're running into similar issues as #1890. I'd suggest overwriting your $PATH environment variable in your .bashrc file or setting the registry key that disables appending the NT path.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss]
"AppendNtPath"=dword:00000000

@Entroper
Copy link
Author

Entroper commented Apr 12, 2017

Amusingly, the strace command worked perfectly -- it ran the Linux npm. I can attach the debug output, but it probably isn't useful. I'll give the $PATH thing a try.

EDIT: Oh, this is interesting. Before I did anything with $PATH or that registry key, I re-ran npm -v to make sure it still didn't work, which it didn't. Then I closed Ubuntu, and re-opened it... and now it works.

@Entroper
Copy link
Author

Since I can no longer reproduce the problem, and pathing issues are being looked into, I'll close this.

@ncla
Copy link

ncla commented Apr 13, 2017

Had the same issue, and closing and opening bash solved that problem. Weird. I did uninstall Node from Windows before-hand.

@samplefrequency
Copy link

I'm running into the exact same issue.

I made sure Node was not installed under Windws and installed Node LTS under WSL. Everything worked perfectly. I was able to run my servers and builds while editing under the Windows environment. Problem is that my IDE has integrations that I would like to be able to use which requires Node for Windows.

I installed Node LTS For Windows and it gets added to the Windows path. Everything works fine under Windows but once I re-open a Bash on Windows instance, NPM utterly fails. Uninstalling Node for Windows and everything under WSL works again.

May someone post the $PATH workaround?

I may try the registry key just to keep the environment paths completely separate as at the moment I don't have a need to launch Windows executables via WSL.

@Entroper
Copy link
Author

I'll reopen this since others are still seeing this issue.

@Entroper Entroper reopened this Apr 15, 2017
@Entroper Entroper changed the title WSL prefers executable in Program Files over /usr/bin WSL prefers executable in Program Files over /usr/bin (NodeJS) Apr 15, 2017
@Entroper Entroper changed the title WSL prefers executable in Program Files over /usr/bin (NodeJS) WSL prefers NodeJS executable in Program Files over /usr/bin Apr 15, 2017
@samplefrequency
Copy link

samplefrequency commented Apr 16, 2017

Re-installed Node for Windows (6.10.2) and this time, I set the installer to not add node and npm to the Windows path. I set my IDE to access the path to Node it requires (c:\program files\nodejs\node.exe) and invoke npm.cmd directly as well if I need to install npm modules in the Windows environment. Not ideal but seems to work for the moment.

Doing the above results in WSL using the proper Linux node and npm installations and Windows will properly use the Windows Node installation as well.

@dfbaskin
Copy link

Same thing, closing Bash and reopening fixed the issue.

@smalike
Copy link

smalike commented Apr 20, 2017

Windows change npm to npm.exe fixed the issue.

@samplefrequency
Copy link

@smalike Do you mean you changed npm.cmd to npm.exe?

@jmalczak
Copy link

@samplefrequency rename npm to npm_cygwin, since npm in program files is a bash script targeted for cygwin

@samplefrequency
Copy link

@jmalczak I ended up simply overriding the PATH variable in WSL and everything is working well now.

@dacp
Copy link

dacp commented Jul 15, 2017

Same, closing and re-opening fixed the issue.

@larscmagnusson
Copy link

larscmagnusson commented Sep 5, 2017

alias npm="path-to-npm-bin-in"
in your .bash_rc works fine for me :)

@Joseph211219479
Copy link

from this post I : uninstalled node from windows, open bash as administer, reinstall node in bash. Thanks all above.

@cenyG
Copy link

cenyG commented Aug 25, 2018

Use nvm for Ubuntu subsystem and all will be cool

@danieliser
Copy link

This isn't so much an issue as a requirement to restart the system or at least the terminal to update PATHs properly

@danieliser
Copy link

Haha found this via google again. Will re-iterate, this isn't a valid issue. PATH variables are cached when terminal/bash/cmd loads, so if you update them you always need to close the open window and open a new bash to access new commands.

Close this.

@hcervantes
Copy link

Simply set your path in WSL
export PATH=/usr/bin:$PATH

@therealkenc
Copy link
Collaborator

therealkenc commented Oct 6, 2020

The OP is missing sudo apt install npm (or was otherwise unavailable at the time in $PATH). On Real Linux people notice this right away (because npm is missing), install npm, and move on. On WSL, if you've installed npm on Windows, but not on your WSL distro, the Windows npm is the one you'll get (because Windows npm is in your $PATH). Same #3882 #1512, others. Pedantically the Syntax error in the OP is linux-behavior /bin/bash like #2365.

There are some suggestions on using node with WSL here.

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

No branches or pull requests