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 and node don't work in wsl if launched in a single command #5251

Closed
robross0606 opened this issue May 22, 2020 · 8 comments
Closed

npm and node don't work in wsl if launched in a single command #5251

robross0606 opened this issue May 22, 2020 · 8 comments

Comments

@robross0606
Copy link

Please fill out the below information:

  • Your Windows build number: (Type ver at a Windows Command Prompt)

Microsoft Windows [Version 10.0.18363.836]

  • What you're doing and what's happening: (Copy&paste the full set of specific command-line steps necessary to reproduce the behavior, and their output. Include screen shots if that helps demonstrate the problem.)

From Windows cmd prompt, type one of the following:

  • wsl.exe npm --version
  • wsl.exe node --version
  • wsl.exe --exec npm --version
  • wsl.exe --exec node --version
  • wsl.exe -- npm --version
  • wsl.exe -- node --version
  • What's wrong / what should be happening instead:

All will result in an error like this:

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

This appears to be trying to launch the Windows native version of the tools instead of launching inside the context of WSL.

detailed_logs.zip

@rescenic
Copy link

Remove NodeJS from Windows system environment first. From what I experienced, Ubuntu WSL read windows system path too (If I read the error in WSL 2 - Failed to translate system path).

@robross0606
Copy link
Author

That's not a viable solution for me. I need node in both environments.

@therealkenc
Copy link
Collaborator

therealkenc commented May 22, 2020

/dupe #3882, #1512 (message) variation #2365 (message), others. You don't have npm installed in your distro, or it is installed but lives in a $PATH component added by your .bashrc (which is not evoked when you run wsl.exe npm).

@ghost
Copy link

ghost commented May 22, 2020

Hi! We've identified this issue as a duplicate of another one that already exists in this repository. This specific instance is being closed in favor of tracking the concern over on the referenced thread.

Thanks for your report!

@ghost ghost added the duplicate label May 22, 2020
@ghost
Copy link

ghost commented May 23, 2020

Hi! We've identified this issue as a duplicate of another one that already exists in this repository. This specific instance is being closed in favor of tracking the concern over on the referenced thread.

Thanks for your report!

@ralemangzz
Copy link

Hi

Can you resolve the problem? I have the same issues, Can you help me?

@therealkenc
Copy link
Collaborator

It appears what is going on here (and elsewhere) is that folks are following the NodeJS on WSL guidance in the docs, which advises using nvm. If you go that route (contrast sudo apt install nodejs npm), then the $PATH component for node and npm is set up via your .bashrc. That means in order to run WSL node from the Windows console with wsl.exe per the OP, you'll need an interactive shell.

$ wsl.exe -d <distroname> /bin/bash -ic "node <node things>"

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

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

5 participants