77

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

14 Answers 14

176

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

7
  • 1
    That worked, but could this potentially cause some other issues down the line?
    – Shadoweb
    Commented Jan 1, 2022 at 15:28
  • 3
    @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?
    – Zymotik
    Commented Jan 2, 2022 at 16:25
  • 1
    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.
    – Shadoweb
    Commented Jan 4, 2022 at 18:36
  • I had this issue but installed nvm and set it up and didn't have to do this work around. github.com/nvm-sh/nvm
    – jkasten
    Commented Apr 21, 2022 at 22:32
  • 3
    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%%')
    – mike
    Commented Aug 2, 2022 at 18:52
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.

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

1
  • This helps me, which npm inside WSL shown that it tried to run the npm version from the /mnt/c/
    – wejoey
    Commented May 11, 2023 at 0:06
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.

1
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

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.
    – Zymotik
    Commented Jan 18, 2022 at 9:17
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

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

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

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
0

You could try either of these:

source ~/.profile or source ~/.bashrc

2
  • 2
    Is there any additional info about what your answer has to offer except just trying two commands?
    – treckstar
    Commented Jun 14, 2022 at 15:48
  • @treckstar 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. Commented Jun 15, 2022 at 17:12
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.

0

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

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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.