Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 474 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appropriate hashbang for Node.js scripts

#1
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?
Reply

#2
If your script is intended for use by Node developers, you should absolutely just use

#!/usr/bin/env node

and not bother trying for compatibility with people who only have Node installed as `nodejs`.

Rationale:

* It's what the cool kids are doing, and if you don't do it too, you're not cool. Major node projects like [jshint](

[To see links please register here]

), [karma](

[To see links please register here]

), [bower](

[To see links please register here]

), and even [npm](

[To see links please register here]

) simply use `#!/usr/bin/env node` as the shebang for their executable scripts.
* Because the cool kids are doing it, anyone who works with Node on Ubuntu has set up a `/usr/bin/node` as a symlink to `nodejs`. There are [highly-viewed instructions][1] on doing this here on Stack Overflow, and all over the web. There was even the `nodejs-legacy` package 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.
* The problem doesn't even seem to exist any more on Ubuntu 14.04; I just purged Node and ran an `apt-get install nodejs` and it created `/usr/bin/node` as 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 the `nodejs-legacy` package to your installation documentation if you deem it necessary. Note that if somebody with `nodejs` but not `node` available tries to run your program with the above shebang, they'll see:

> /usr/bin/env: node: No such file or directory

and [Googling that][2] 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 `nodejs` is available but `node` is not (or where `node` is actually the [Amateur Packet Radio Node program](

[To see links please register here]

)), then you can use this "two-line shebang" taken from [Unix & Linux Stack Exchange][3]:

#!/bin/sh
':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@"

console.log('Hello world!');

but do you really need to do this when almost nobody else in the Node world is?

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through