Zymotik
Zymotik

Reputation: 7357

After installing npm on WSL Ubuntu 20.04 I get the message "/usr/bin/env: ‘bash\r’: No such file or directory"

I see the following message when running the npm install or npm command from the terminal. Executing node works as expected.

    > npm install
    /usr/bin/env: ‘bash\r’: No such file or directory

Upvotes: 77

Views: 71338

Answers (14)

Fox
Fox

Reputation: 1

I was getting this issue on WSL while trying to use asdf to install a particular version of nodejs. The install was failing due to

‘bash\r’: No such file or directory

Tried the options listed in the other comments but none of them worked. Eventually I worked out that I needed to uninstall an existing version of nodejs which I'd had prior to using asdf.

sudo apt remove nodejs

After this, the asdf install command ran without problems and installed the correct version of nodejs for the project I was working on.

Upvotes: 0

Zymotik
Zymotik

Reputation: 7357

Update July 2023: Microsoft has a guide for installing Node on WSL using NVM. You likely won't have this issue if you follow their how to.

The fix that has worked for many: it may be a line endings issue causing problems, but not from Ubuntu. Make sure you have node and npm installed correctly:

  1. From WSL run sudo apt install nodejs npm to install node & npm
  2. From PowerShell/CMD run wsl --shutdown to restart the WSL service
  3. From WSL run which npm to confirm it's installed (output should be: /usr/bin/npm)

Does the problem persist? Try this next:

Stop Windows path variables being shared with WSL by editing the /etc/wsl.conf file in WSL. If the file doesn't exist, execute sudo touch /etc/wsl.conf first. Edit the file with the command sudo nano /etc/wsl.conf and add the following configuration:

[interop]
appendWindowsPath = false

Then restart WSL2 with command wsl --shutdown in Windows.

Note 1: This will stop the PATH environment variables from Windows passing through to WSL. Known bug: this stops the VSCode code . command from working in WSL. If this is a problem, use NVM solution described here or switch to using node in a docker container.

Note 2: this also affects pyenv command, see /usr/bin/env: ‘bash\r’: No such file or directory: Incompatible line-endings (WSL?)

Tip from @mike: "I did not want to disable the ability to do code . so I just removed the windows nodejs path by adding this line to my ~/.bashrc PATH=$(echo "$PATH" | sed -e 's%:/mnt/c/Program Files/nodejs%%')"

Upvotes: 176

jasonph
jasonph

Reputation: 157

I think the better solution would be installing Node using nvm on WSL, then you won't have this problem.

Installing Nodejs and npm using nvm for Ubuntu 20.04

Upvotes: 3

Dm Mh
Dm Mh

Reputation: 840

In my case, sudo did the trick. The issue is not fixed, but at least for now, I can move on.

Before:

 npm init -y

After:

sudo npm init -y

Upvotes: 1

nlewis99
nlewis99

Reputation: 89

This sounds like the underlying problem is that you installed node using the Nodejs Windows Installer online. The easiest solution to this problem when working with Windows subsystem for Linux is to perform the initial Nodejs install not using the Nodejs Windows Installer, but rather the following process on your WSL command line:

Open a new WSL Terminal

To enable the download of content from the internet via Ubuntu:

$ sudo apt-get install curl

Then to install nvm (Note: check the latest version of nvm and substitute into the path accordingly; as of this post v0.39.1 is the latest):

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

To verify the installation of nvm (Note: you may need to close and reopen your terminal - should return "nvm"):

$ command -v nvm

Install the current stable release of Node.js:

$ nvm install --lts

To confirm you now have node and npm installed:

$ node --version && npm --version

And you should see something like:

v16.16.0

8.11.0

It may be beneficial to uninstall your current Nodejs installation and reinstall using the method above. This will save a lot of headaches trying to understand other potential work arounds. Note: the above solution only applies to WSL. Further details can be found here on the Microsoft docs.

Upvotes: 3

shubhranka varma
shubhranka varma

Reputation: 53

You might have other version of node in your windows machine.

brew link node@<Your Version> or brew link node

Upvotes: 0

Alex Oakley
Alex Oakley

Reputation: 127

It's possible that npm has been installed in the wrong place (e.g., in Windows rather than Ubuntu). To check, try running which npm.

I had a similar problem which I fixed by restarting my shell after running

$ sudo apt install npm.

Now, when I run

$ which npm

gives:

/usr/bin/npm

Upvotes: 1

Richard
Richard

Reputation: 16812

The accepted answer didn't work me. I found it was simply that I had some .js files that had Windows line endings instead of Linux.

The easiest way to fix this was rm -rf node_modules && npm update in my project folder to get Linux versions.

Upvotes: 0

Infomal Haut
Infomal Haut

Reputation: 29

You could try either of these:

source ~/.profile or source ~/.bashrc

Upvotes: 0

Kimbo
Kimbo

Reputation: 84

I had this exact issue as well, running on same distro & version. After reading everyone's comments, I figured the easiest solution to try first was to 'turn it off and on again' and it worked! I ran this in a windows (not ubuntu) terminal.

wsl --shutdown

Upvotes: 6

E.Cruz
E.Cruz

Reputation: 11

This is a solution that uses NVM, a version manager for the node framework. Suggested solution:

Install curl in WSL for downloading from the internet:

sudo apt-get install curl

Next get the NVM downloader script and run it with bash:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Next, install the long term support version of NVM:

nvm install --lts

Finally, install node:

nvm install node

You can find more information about NVM in the WSL documentation

Upvotes: 1

lipengtao
lipengtao

Reputation: 51

Nodejs has been installed outside the WSL, so I typed the NPM command in the WSL window and actually called the NPM on Windows. After uninstalling both Windows and WSL nodes, I reinstalled the NODE in WSL and there is no problem

Upvotes: 5

fsarter
fsarter

Reputation: 942

I hit the same issue, after install nodejs and npm, just restart my wsl with wsl --shutdown solved the issue, you can try this.

Upvotes: 13

stkofler
stkofler

Reputation: 11

This happened to me today after I updated node and npm through nvm. I realized that indeed the .nvm path was missing from echo $PATH. Then I noticed that npm use v16.14.0 would re-add it to $PATH and make npm i work again. However, this only worked for the current tab and to make it work in new sessions/tabs I had to run:

nvm alias default v16.14.0

Upvotes: 1

Related Questions