适用于Node.js脚本的hashbang

编程入门 行业动态 更新时间:2024-10-24 20:20:56
本文介绍了适用于Node.js脚本的hashbang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

#!/usr/bin/env nodejs

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

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).

是否有任何方法可以指定备份hashbang或与node.js兼容的备份哈希?

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

推荐答案

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

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

#!/usr/bin/env node

,并且不为与仅将Node安装为nodejs的用户兼容而烦恼.

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

理论上:

  • 这是很酷的孩子们正在做的事情,如果您也不这样做,那么您就不会很酷.主要节点项目,例如 jshint ,业力, bower ,甚至 npm 只需将#!/usr/bin/env node用作其可执行脚本的工具即可.
  • 由于很酷的孩子正在这样做,因此在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的符号链接.我怀疑受此问题困扰的人正在减少.
  • 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 npm 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 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.

即使您的目标读者是不识字的人,您仍然可能希望使用#!/usr/bin/env node,如果您认为有必要,可能还会在安装文档中添加手动创建符号链接或安装nodejs-legacy软件包的可能. .请注意,如果具有nodejs但没有node可用的人尝试使用上述shebang运行您的程序,他们会看到:

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:节点:没有这样的文件或目录

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

和对此进行搜索,他们会在第一个结果中并多次在第一页中为他们提供修复.

and Googling that will give them the fix in the first result and many times on the first page.

如果确实是,您绝对希望确保用户可以在有nodejs可用但没有node的系统上(或者node实际上是业余分组无线节点程序),则可以使用从Unix& Linux Stack Exchange :

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), then you can use this "two-line shebang" taken from Unix & Linux Stack Exchange:

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

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

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

更多推荐

适用于Node.js脚本的hashbang

本文发布于:2023-11-28 19:42:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   脚本   js   Node   hashbang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!