使Node.js支持JavaScript文件的shebang(#!) [英] Make Node.js support the shebang (#!) for JavaScript files

查看:93
本文介绍了使Node.js支持JavaScript文件的shebang(#!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些脚本语言(例如Python或Bash)使用#进行注释.

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

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

我可以运行脚本:

python script.py

./script.py

是否可以使JavaScript支持shebang?

Is it possible to make JavaScript support shebang?

推荐答案

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

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

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

这篇关于使Node.js支持JavaScript文件的shebang(#!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆