2

I have just created an Ubuntu 18.04 instance on AWS and copied an existing project over. The usual shebang line for Node #!/usr/bin/env node now doesn't work. It gave:

 #!/usr/bin/env: No such file or directory

What could be wrong?

My $PATH variable is:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

which node gives: /usr/bin/node

which nodejs gives /usr/bin/nodejs

ls -l nodejs gives: /usr/bin/nodejs -> /etc/alternatives/nodejs

ls -l /usr/bin/env gives:

-rwxr-xr-x 1 root root 35000 Jan 18  2018 /usr/bin/env
Old Geezer
  • 619
  • 3
  • 7
  • 14

1 Answers1

2

The script that gives this error message got mangled somehow. For instance, if you did some touchup of your script in a Windows editor before shipping it to your AWS instance, your script now likely starts with a non-printing BOM before the shebang, and maybe all your script's lines now end in Microsoft's favorite CR-LF combo instead of the traditional Unix LF.

Check the output of this command on your AWS instance:

$ LC_ALL=C sed -n 1l myscript.sh

That tells sed to print the first line of your script, but escape all non-printing characters and end the line with a $ character (in case there are invisible spaces at the end of the line).

There's only one correct output:

#!/usr/bin/env node$

If what you see doesn't look exactly like that, run the same command on your original myscript.sh. If you get the same result, fix it there, then copy it over to AWS again.

Adrian
  • 166
  • 4
  • Spot on! Thanks. It seems to affect only the shebang line. Most of my other `js` files have the BOM and Windows line endings and they work fine. – Old Geezer Feb 13 '19 at 06:08
  • @OldGeezer That's probably because all your `.js` files are being read by a running Node process, which doesn't have problems with BOMs and Windows line endings. The script you're having problems with starts that Node process, and is therefore read by the Linux kernel to determine how to run it. That's why it has to play by Unix rules: ASCII shebang line, no BOM or prepended space, Unix line ending (the kernel ignores everything after the shebang line). – Adrian Feb 13 '19 at 06:17