Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

npm or a script fails in WSL with /usr/bin/env: 'bash\r'

Error/usr/bin/env: 'bash\r': No such file or directory

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

  • Applies to WSL 2, Ubuntu 24.04, Node.js 22, nvm
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 2
  2. 2Match what you see, then jump to its fix

    1. 1command -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. 2npm 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

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 npm

2Fix

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

3Verify

npm must now resolve to a Linux path and run.

command -v npm
npm --version

Confirmed by learn.microsoft.com

Did this fix it?

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_modules

2Fix

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

Confirmed by git-scm.com

Did this fix it?

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

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.