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

This is a line endings issue, but not from Ubuntu. Make sure you do have node and npm installed correctly by using the command:

sudo apt install nodejs npm

Then 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: this also affects pyenv command, see /usr/bin/env: ‘bash\r’: No such file or directory: Incompatible line-endings (WSL?)