bash 使 Node.js 支持 JavaScript 文件的 shebang (#!)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10696222/
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-18 02:21:28  来源:igfitidea点击:

Make Node.js support the shebang (#!) for JavaScript files

javascriptnode.jsbashshebang

提问by kev

Some scripting languages (such as Python or Bash) use #for comments.

某些脚本语言(例如 Python 或 Bash)#用于注释。

#!/usr/bin/env python
print 'hello, world'

I can run the script:

我可以运行脚本:

python script.py

Or

或者

./script.py

Is it possible to make JavaScript support shebang?

是否可以让 JavaScript 支持 shebang?

回答by ThiefMaster

Yes, you can simply use #!/usr/bin/env node(or whatever the name of your JavaScript interpreter is, it works fine with js(spidermonkey), too).

是的,您可以简单地使用#!/usr/bin/env node(或者无论您的 JavaScript 解释器的名称是什么,它也可以与js(spidermonkey)一起使用)。

[me@hades:~]> cat > test.js
#!/usr/bin/env node
console.log('hi');
[me@hades:~]> chmod +x test.js
[me@hades:~]> ./test.js
hi

Most likely both interpreters test if the first line begins with #!and in this case it is skipped.

很可能两个解释器都会测试第一行是否以 开头,#!在这种情况下它会被跳过。