I'm trying to create a script for node.js that will work in multiple environments. Particularly for me, I'm switching back and forth between OS X and Ubuntu. In the former, Node is installed as node, but in the latter it is nodejs. At the top of my script, I can have:
#!/usr/bin/env node
or
#!/usr/bin/env nodejs
I'd rather have the script run as an executable for either environment as long as node is installed rather than have one or the other have to specify the command (./script-name.js vs. node script-name.js).
Is there any way to specify a backup hashbang or one that is compatible in either case for node.js?
If your script is intended for use by Node developers, you should absolutely just use
and not bother trying for compatibility with people who only have Node installed as
nodejs.Rationale:
#!/usr/bin/env nodeas the shebang for their executable scripts./usr/bin/nodeas a symlink tonodejs. There are highly-viewed instructions on doing this here on Stack Overflow, and all over the web. There was even thenodejs-legacypackage whose entire purpose was to create this symlink for you. People who use Node know how to fix this problem on Ubuntu, and they have to if they want to use pretty much any software ever written in Node.apt-get install nodejsand it created/usr/bin/nodeas a symlink to/etc/alternatives/node. People afflicted by this issue are, I suspect, a shrinking minority.Even if you're targeting Node-illiterate people, you may still want to use
#!/usr/bin/env node, perhaps adding the possible need for manual symlink creation or installation of thenodejs-legacypackage to your installation documentation if you deem it necessary. Note that if somebody withnodejsbut notnodeavailable tries to run your program with the above shebang, they'll see:and Googling that will give them the fix in the first result and many times on the first page.
If you truly, desperately want to make sure that the user can run your software on a system where
nodejsis available butnodeis not (or wherenodeis actually the Amateur Packet Radio Node program), then you can use this "two-line shebang" taken from Unix & Linux Stack Exchange:but do you really need to do this when almost nobody else in the Node world is?