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

14

76

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
Shep answered 11/6, 2021 at 14:7 Comment(0)
S
175

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%%')"

Shep answered 11/6, 2021 at 14:7 Comment(7)
That worked, but could this potentially cause some other issues down the line?Dispassionate
@shadoweb it will stop the Windows path being appended to the WSL path. Where you rely on a Windows path being available in WSL, this will no longer be available. I can't think of any instances where this will cause a problem, maybe the Stack overflow community can help?Shep
OK so it does cause an issue with the shortcut command code . but it seems that deactivating the above doesn't break the npm install anymore, so I'll leave it like that.Dispassionate
I had this issue but installed nvm and set it up and didn't have to do this work around. github.com/nvm-sh/nvmUnthread
Thanks @zymotik for explaining the problem I was wondering how my WSL got all those windows directories. I just ran into the same issue after installing a newer version of of nodejs . 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%%')Goodish
in case service not starting again use Get-Service vmcompute | Restart-ServicePer
Unsharing the PATHvariable has helpedSophistication
M
13

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

Michamichael answered 26/3, 2022 at 7:19 Comment(0)
O
6

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
Odin answered 7/6, 2022 at 3:42 Comment(0)
H
4

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

Hydroxylamine answered 30/3, 2022 at 8:28 Comment(1)
This helps me, which npm inside WSL shown that it tried to run the npm version from the /mnt/c/Digital
G
3

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

Gretchengrete answered 16/1, 2022 at 23:4 Comment(1)
...or if you are used to using Docker, you could run your commands inside a container using a pattern such as the three musketeers. This will give you consistency across platforms, control of versions and confidence it will work the same in dev as in your pipelines.Shep
R
3

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.

Rondi answered 11/8, 2022 at 15:6 Comment(1)
I had nvm getting rid of it was a journey, but thanks to github.com/nvm-sh/nvm/issues/298 for doing rm -rf ~/.nvmSpicule
C
1

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

Coo answered 7/3, 2022 at 16:35 Comment(0)
E
1

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

Eula answered 30/5, 2022 at 22:5 Comment(0)
C
1

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

Cnut answered 8/6, 2022 at 5:58 Comment(0)
M
1

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
Marplot answered 19/2, 2023 at 17:4 Comment(0)
P
0

You could try either of these:

source ~/.profile or source ~/.bashrc

Primo answered 14/6, 2022 at 15:5 Comment(2)
Is there any additional info about what your answer has to offer except just trying two commands?Vitavitaceous
@Vitavitaceous When you install npm, the npm path will be added to .bashrc. And when you close and open the wsl terminal again, the .bashrc file wont run. So you use the command to run the file.Primo
H
0

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.

Higdon answered 21/6, 2022 at 9:7 Comment(0)
D
0

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

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

Downes answered 11/7, 2022 at 7:52 Comment(0)
P
0

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.

Pinnate answered 23/4 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.