适用于 Node.js 脚本的 hashbang

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20638520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:31:04  来源:igfitidea点击:

Appropriate hashbang for Node.js scripts

node.jsshellshebang

提问by Explosion Pills

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:

我正在尝试为 node.js 创建一个可以在多种环境中工作的脚本。特别是对我来说,我在 OS X 和 Ubuntu 之间来回切换。在前者中,Node 安装为node,但在后者中安装为nodejs. 在我的脚本顶部,我可以有:

#!/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.jsvs. node script-name.js).

只要安装了节点,我宁愿让脚本作为任一环境的可执行文件运行,而不是让一个或另一个必须指定命令(./script-name.jsvs. node script-name.js)。

Is there any way to specify a backup hashbang or one that is compatible in either case for node.js?

有什么方法可以为 node.js 指定备份 hashbang 或在任何一种情况下都兼容的 hashbang 吗?

回答by Mark Amery

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

如果您的脚本旨在供 Node 开发人员使用,则您绝对应该使用

#!/usr/bin/env node

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

并且不用费心去尝试与只将 Node 安装为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, karma, bower, and even npmsimply use #!/usr/bin/env nodeas 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/nodeas a symlink to nodejs. There are highly-viewed instructionson doing this here on Stack Overflow, and all over the web. There was even the nodejs-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 toif 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 nodejsand it created /usr/bin/nodeas a symlink to /etc/alternatives/node. People afflicted by this issue are, I suspect, a shrinking minority.
  • 这就是酷孩子们正在做的事情,如果你不这样做,你就不酷。像jshintkarmabower甚至npm这样的主要节点项目只是#!/usr/bin/env node用作其可执行脚本的shebang。
  • 因为很酷的孩子正在这样做,任何在 Ubuntu 上使用 Node 的人都设置了/usr/bin/node一个符号链接到nodejs. 有高度看说明书上堆栈溢出这里这样做,并在网上所有。甚至还有一个nodejs-legacy包,其全部目的是为你创建这个符号链接。使用 Node 的人知道如何在 Ubuntu 上解决这个问题,如果他们想使用几乎任何用 Node 编写的软件,他们就必须这样做
  • 这个问题在 Ubuntu 14.04 上似乎不再存在;我刚刚清除了 Node 并运行了一个,apt-get install nodejs并将它创建/usr/bin/node/etc/alternatives/node. 受此问题困扰的人,我怀疑,是一个正在萎缩的少数人。

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-legacypackage to your installation documentation if you deem it necessary. Note that if somebody with nodejsbut not nodeavailable tries to run your program with the above shebang, they'll see:

即使您的目标是不了解 Node 的人,您可能仍然想要使用#!/usr/bin/env nodenodejs-legacy如果您认为有必要,可能会将手动创建符号链接或安装包的可能需要添加到您的安装文档中。请注意,如果有nodejs但不可node用的人尝试使用上述shebang运行您的程序,他们将看到:

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

/usr/bin/env: node: 没有那个文件或目录

and Googling thatwill 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 but nodeis not (or where nodeis actually the Amateur Packet Radio Node program), then you can use this "two-line shebang" taken from Unix & Linux Stack Exchange:

如果您真的非常想确保用户可以在nodejs可用但node不可用的系统上运行您的软件(或者node实际上是业余分组无线电节点程序),那么您可以使用这个“双线shebang”来自Unix 和 Linux 堆栈交换

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

console.log('Hello world!');

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

但是当 Node 世界中几乎没有其他人时,您真的需要这样做吗?