npm or a script fails in WSL with /usr/bin/env: 'bash\r'
/usr/bin/env: 'bash\r': No such file or directoryShort answer
The \r in the message is a Windows line ending at the end of a script's first line, so Linux looks for a program called bash followed by a carriage return. Either WSL is running the Windows copy of npm, or one of your own scripts was saved with CRLF. Find the file that failed, then install Node inside WSL or convert the script to LF.
Fix it fast
1Find out which cause you have
Find out which file produced the message. Check where npm comes from, and look at the raw bytes of its first line. A \r before the \n at the end means the script has Windows line endings.
command -v npm node head -n 1 "$(command -v npm)" | od -c | head -n 22Match what you see, then jump to its fix
- 1
command -v npm prints a path starting with /mnt/c/, and the od output shows \r before the final \nWSL runs the Windows copy of npm, not a Linux oneFix - 2
npm resolves to a Linux path, but the error appears when you run ./deploy.sh or an npm script that calls a .sh fileOne of your own scripts was checked out with CRLF line endingsFix
- 1
What does the error look like?
/usr/bin/env: 'bash\r': No such file or directory
Cause 1 of 2
WSL runs the Windows copy of npm, not a Linux one
1Check
WSL can append the Windows PATH to the Linux PATH (the appendWindowsPath setting is on by default). If Node.js is not installed inside the distro, the first npm WSL finds can be the one in Program Files. Its launcher script then fails on the \r when it carries Windows line endings, which the od check above shows.
echo "$PATH" | tr ':' '\n' | grep '^/mnt/'
command -v npm2Fix
Install Node.js inside the distro with a version manager, after removing any apt-installed Node.js or npm. Then confirm npm resolves to your Linux home folder. If it still resolves to /mnt/c, stop WSL from adding the Windows PATH by setting appendWindowsPath to false in /etc/wsl.conf, and restart WSL from PowerShell with wsl --shutdown. With that setting off, Windows programs such as explorer.exe need their full path.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
# open a new terminal, then:
nvm install --lts
# only if npm still resolves to /mnt/c: sudo nano /etc/wsl.conf
[interop]
appendWindowsPath=false3Verify
npm must now resolve to a Linux path and run.
command -v npm
npm --versionConfirmed by learn.microsoft.com
Cause 2 of 2
One of your own scripts was checked out with CRLF line endings
1Check
Git for Windows can write text files with CRLF when it checks them out, and the repo you open in WSL may come from there. Without a text or eol attribute, Git decides from the core.autocrlf setting. Look for carriage returns in the script that failed.
head -n 1 deploy.sh | od -c | head -n 2
grep -rl $'\r' --include='*.sh' . --exclude-dir=node_modules2Fix
Strip the carriage returns from the files you have now, then tell Git to keep shell scripts as LF in the working tree. The eol=lf attribute does that for matching files, and git add --renormalize re-adds files so the index is normalized too.
sed -i 's/\r$//' deploy.sh
# .gitattributes
*.sh text eol=lf
git add --renormalize .
git commit -m "Keep shell scripts as LF"3Verify
The first line of the script must end in a plain newline, and the script must start.
head -n 1 deploy.sh | od -c | head -n 2
./deploy.shConfirmed by git-scm.com
How do I stop it coming back?
- Keep Node.js, npm and your projects inside the Linux file system of the distro, and install Node.js there with nvm.
- Commit a .gitattributes file with eol=lf for every file the Linux side executes: *.sh, and any extensionless launcher scripts.
- Edit scripts in an editor set to LF endings, or open the project through VS Code's WSL connection so it runs on the Linux side.
- After changing wsl.conf, run wsl --shutdown and wait a few seconds before reopening the distro.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
npm and yarn are many times slower in WSL than on plain Windows
pwd prints /mnt/c/Users/...
Almost always the project lives on the Windows drive (/mnt/c), and every file read crosses between the two systems.
2 causes, each with a fixBack end
node fails in WSL with exec format error
zsh: exec format error: node
The node that zsh finds is probably a Windows program on the PATH, and is not a Linux program.
2 causes, each with a fixBack end
The nest command is not found in the terminal
bash: nest: command not found
The shell cannot find a nest executable on its PATH.
3 causes, each with a fixBack end
npm install worked but the command is not found
bash: vite: command not found
npm puts a local install's command in the project's node_modules/.bin, which is not on your shell PATH, so run it with npx or from an npm script.
3 causes, each with a fixBack end
node and npm vanish or are not found when nvm is installed
nvm current prints system in a new shell
nvm adds node and npm to PATH only for the version it has activated.
4 causes, each with a fixBack end
The serverless command is not found in the shell
bash: serverless: command not found
The shell can only run serverless if it is installed globally and the folder npm links global commands into is on PATH.
3 causes, each with a fix