41

I've been using NPM on my machine (Windows 10), but recently ran into an issue. I currently have Node.js installed and get the following error while running any npm command.

Question: What is causing this error and whats the best way to resolve it.

Command:

$ npm install

Output/Error:

bash: /c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory
ChaseHardin
  • 2,189
  • 7
  • 29
  • 50
  • Possible duplicate of [./configure : /bin/sh^M : bad interpreter](http://stackoverflow.com/questions/2920416/configure-bin-shm-bad-interpreter) – Mark Chorley Sep 03 '16 at 20:54
  • @MarkChorley yes, I had already previously viewed that link. I tried the commands, but couldn't get my error resolved. – ChaseHardin Sep 03 '16 at 20:56
  • Are you getting these errors in a specific project? If so the rogue character is in there somewhere, perhaps in package.json? – Mark Chorley Sep 03 '16 at 20:59
  • No, it happens for all projects. I've navigated to multiple directories to verify. – ChaseHardin Sep 03 '16 at 21:01
  • 2
    Everything on Google seems to suggest that its a linux error, but I'm running windows 10. – ChaseHardin Sep 03 '16 at 21:01
  • Which shell are you running this in? What version of node and npm do you have? Consider upgrading them if they're out of date? – Mark Chorley Sep 03 '16 at 21:04
  • 1
    @MarkChorley thanks for the help. Previously, I had been running my npm commands in git bash, but tried it in cmd and it works. – ChaseHardin Sep 03 '16 at 21:14

13 Answers13

114

Not my solution, but this seemed to work for me. Appears that having the Windows folder structure in $PATH while using WSL2 was causing that parse error, but I'm not exactly sure why.

  1. Go to your user root (cd ~)
  2. Open .bashrc in your chosen editor (vi, nano, etc.)
  3. Append to the end of the file: export PATH=$(echo "$PATH" | sed -e 's/:\/mnt[^:]*//g') # strip out problematic Windows %PATH%
  4. Close and re-open all terminal windows

Source: https://hackmd.io/@badging/wsl2#Troubleshooting-PATH


Updated: Per Lh Lee's comment, I've updated the regex from s/:\/mnt.*//g to s/:\/mnt[^:]*//g as this avoids accidentally capturing anything extra after the problematic /mnt paths.

Whereas the first regex will match /mnt/c/blah:/other/thing, the new one will not.

  • 8
    helped me too. I'm using WSL 2 – Victor Gorban Oct 13 '20 at 08:17
  • 4
    I came across this problem in WSL2 too. You can check whether if you install `npm` in WSL first by typing `which npm`. If it shows `/usr/bin/npm`, that it means you've installed `npm`. And for temporary use, just then type `/usr/bin/npm install mypackge`. – Rick Nov 02 '20 at 10:16
  • I am using WSL2 and that particular "error" message went away. However, Now I get "Command 'npm' not found, but can be installed... ". Here as follows: `` spiderman2021@David-Win10-PC:~/projects$ npm init -y Command 'npm' not found, but can be installed with: sudo apt install npm `` – Manuel Hernandez Feb 20 '21 at 18:11
  • 1
    This is a fantastic human being for knowing this. – Meow Meow Apr 26 '21 at 02:58
  • 1
    This strips out everything that starts with `/mnt` in your path. Use this instead: `export PATH=$(echo "$PATH" | sed -e 's/:\/mnt[^:]*//g') # strip out problematic Windows %PATH%` – Lh Lee May 12 '21 at 16:40
  • Updated per the above comment! :) – Adam 'Crashdoom' Walker May 13 '21 at 16:44
  • This appears to break other commands in PATH still. E.G. "code" no longer works after I ran this, however, npm now does (so still upvoted) – FredM Jun 09 '21 at 20:30
  • i used it and worked fine... but yesterday 20.10.2021 was update for win 11 prerelease and they repair it on their side, i got same error again than i just removed this added line and all good – Lukáš Secký Oct 21 '21 at 05:48
12

Install nodejs/npm using nvm and it will not conflict with the one in windows. The path for npm becomes (after installing using nvm) /home/ubuntu/.nvm/versions/node/v14.16.0/bin/npm

Read More about setting up your Node.js development environment with WSL 2

Aakash
  • 121
  • 1
  • 2
  • I still don't understand how Microsoft is suggesting `nvm` is a real installation strategy. `nvm` is an anti-pattern. I don't need another tool for installing and managing installed programs. `apt` should work. – Cameron Tacklind Aug 10 '21 at 02:39
  • Doesn't work when you're using tools and programs that are calling npm themselves. – Jasmin Parent Jan 07 '22 at 14:10
8

I did with these commands.

sudo apt update && sudo apt install curl -y

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 

source ~/.profile 

nvm ls-remote 

nvm install v15 

node --version 
Aditya Malviya
  • 1,907
  • 1
  • 20
  • 25
3

I had the same issue, I solved it by installing my dependencies on my Linux sub system as well. In my case it was just nodejs and npm.

  1. Open Ubuntu terminal(Windows Sub system) I'm on windows 10...

    sudo apt update

  2. Now that the system is up to speed, let's do...

    sudo apt install nodejs npm

(Do a variation of this suitable to you, I assume most are using nodejs though.)

My npm works now but still gives the error first. I mean it's not aesthetic at first, but my terminal on VS Code doesn't have the error first. Which is awesome!

TLDR: The linux subsystem doesn't have npm so download it there first.

Edit: formatting

ZaneK
  • 301
  • 2
  • 14
3

This command with 78 upvotes:

# export PATH=$(echo "$PATH" | sed -e 's/:\/mnt[^:]*//g') # strip out problematic Windows %PATH%

causes problems with other commands such as the "code" command for VS Code. It manipulates the $PATH. The correct solution here is to use nvm:

sudo apt-get install curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
nvm ls
nvm use 14
Daniel Viglione
  • 8,014
  • 9
  • 67
  • 101
3

It happened to me just now and all I had to do was exiting my wsl2 session and login in again.

Laercio Metzner
  • 1,351
  • 11
  • 22
1

Just faced the same issue, this issue happens because npm is installed on your windows machine but not on your WSL one. you just need to install npm on your linux machine then it will read the binary from linux not windows that's in case you want to use windows paths on your WSL. otherwise if you don't need the windows paths you can use thing adam mentioned by adding this into your path: PATH=$(echo "$PATH" | sed -e 's/:\/mnt.*//g') then refresh the shell with source ~/.bashrc

1

I encountered the same problem and found the cause and a simple solution.

After installing nvm in bash a few months ago, I recently decided to give zsh and on-my-zsh a try. I followed the instructions and installed zsh and oh-my-zsh. When trying to run node or npm I got the errror:

zsh: /mnt/c/Program Files/nodejs/npm: bad interpreter: /bin/sh^M: no such file or directory

My investigations led me to the $PATH variable. I then compared the output of echo $PATH in bash and zsh. In bash the path included the nvm directory, in zsh this directory was not added to the path.

The reason for this difference is that nvm adds a snippet to the end of .bashrc. In zsh .zshrc is loaded instead and the snippet will not be executed.

The snippet looks like this:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

You can just copy these lines from .bashrc to the end of .zshrc, restart the shell, and the issue should be fixed if you have the same problem.

Andreas Riedmüller
  • 1,217
  • 13
  • 17
1

If you already installed everything you need following this link https://learn.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-wsl just try restarting your VS Code and trying to run npm install.

Chaka15
  • 1,105
  • 1
  • 8
  • 24
1

I got this error when running npm install in a Visual Studio Code terminal, using Powershell. I switched to use a WSL2 terminal inside Visual Studio Code instead and it worked (tip - use npm i as a shorthand)

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
0

I am using Git Bash for cli

I my case my Antivirus has quarantined the C:\Program Files\Git\user\bin\sh.exe file, and that is why $ npm init was not working and showing this error:

bash: /c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory

When I restored that file it started working normally.

Theo
  • 57,719
  • 8
  • 24
  • 41
mak
  • 1
0

experienced this problem on my wsl2

not a fix but a workaround. unmounting the c drive resolve the issue.

umount /mnt/c

Julius Limson
  • 526
  • 9
  • 29
  • Then I get the message: ``-bash: /mnt/c/Program Files/nodejs/npm: No such file or directory``. – ikreb Feb 19 '21 at 08:10
  • @ikreb It's trying to run the npm that's installed in Windows. did you install your npm inside the wsl2? – Julius Limson Feb 26 '21 at 04:08
  • 5
    This is a bad answer. Unmounting a drive is not viable if you're working on a repo from within that drive. – AyesC Mar 18 '21 at 01:19
0

I had the same problem when I started using WSL, I solved it by using npm.cmd instead of npm command.

Marfullsen
  • 21
  • 3
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33344687) – Sidharth Mudgil Dec 11 '22 at 17:26