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
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:
sudo apt install nodejs npm to install node & npmwsl --shutdown to restart the WSL servicewhich 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%%')"
code . but it seems that deactivating the above doesn't break the npm install anymore, so I'll leave it like that.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
which npm inside WSL shown that it tried to run the npm version from the /mnt/c/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.
nvm getting rid of it was a journey, but thanks to github.com/nvm-sh/nvm/issues/298 for doing rm -rf ~/.nvmI think the better solution would be installing Node using nvm on WSL, then you won't have this problem.
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
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
You could try either of these:
source ~/.profile
or
source ~/.bashrc
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.