Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot run NPM Commands

Tags:

node.js

npm

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
like image 341
ChaseHardin Avatar asked Sep 03 '16 20:09

ChaseHardin


People also ask

Why npm commands are not working?

On Windows, the cause of this error could be that a PATH or system variable is not correctly set. The error can also occur if you do not have npm or Node. js installed, have an outdated version, or have permission issues.

Why npm command is not working in VS code?

If you're calling npm from the VSCode terminal, you need to restart VSCode first before trying again. If you still get the error, then try restarting your computer first. The error should be gone after you restart. Now you should be able to install any npm package to your local computer with npm install command.

How do I fix npm not found?

The npm command not found error js server, which you can download from the nodejs.org website. Once you downloaded and installed Node. js, open the terminal and run the npm -v command. Once you see the npm version, you should be able to run the npm install command again.


10 Answers

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.

like image 118
Adam 'Crashdoom' Walker Avatar answered Oct 05 '22 22:10

Adam 'Crashdoom' Walker


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

like image 20
Aakash Avatar answered Oct 05 '22 22:10

Aakash


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 
like image 36
Aditya Avatar answered Oct 05 '22 21:10

Aditya


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

like image 20
ZaneK Avatar answered Oct 05 '22 20:10

ZaneK


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
like image 23
Daniel Viglione Avatar answered Oct 05 '22 21:10

Daniel Viglione


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

like image 45
Mohamed Dief Avatar answered Oct 05 '22 20:10

Mohamed Dief


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.

like image 22
Andreas Riedmüller Avatar answered Oct 05 '22 22:10

Andreas Riedmüller


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

like image 30
Chaka15 Avatar answered Oct 05 '22 21:10

Chaka15


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

like image 21
Laercio Metzner Avatar answered Oct 05 '22 20:10

Laercio Metzner


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)

like image 42
Chris Halcrow Avatar answered Oct 05 '22 22:10

Chris Halcrow