Node.js is very popular these days and I've been writing some scripts on it. Unfortunately, compatibility is a problem. Officially, the Node.js interpreter is supposed to be called node
, but Debian and Ubuntu ship an executable called nodejs
instead.
I want portable scripts that Node.js can work with in as many situations as possible. Assuming the filename is foo.js
, I really want the script to run in two ways:
./foo.js
runs the script if eithernode
ornodejs
is in$PATH
.node foo.js
also runs the script (assuming the interpreter is callednode
)
Note: The answers by xavierm02 and myself are two variations of a polyglot script. I'm still interested in a pure shebang solution, if such exists.
node
for your script, or having a kind of make script that modifies the shebang.alphacentauri
and such. If there's an executable callednodejs
, you can be 99% sure it's Node.js. Why not support bothnodejs
andnode
?