是否可以运行Node.js脚本而不调用`node`? [英] Is it possible to run Node.js scripts without invoking `node`?

查看:176
本文介绍了是否可以运行Node.js脚本而不调用`node`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢javascript,所以当我听说 Node.js (一个基于V8的Javascript运行时)时,我很兴奋。我喜欢在Javascript中做我的shell脚本。我的问题是这样:我如何运行我的脚本而不调用 node〜/ Scripts / myscript.js ?在我<@ c $ c> chmod + x 我的脚本后,它尝试作为bash脚本而不是Node.js javascript。

I like javascript, so I was excited when I heard about Node.js, a V8-based Javascript runtime. I would prefer to do my shell scripting going forward in Javascript. My issue is this: how can I run my scripts without calling node ~/Scripts/myscript.js? After I chmod +x my script, it tries to run as a bash script instead of a Node.js javascript.

解决方案

什么使你当前的shell启动bash是你当前的shell(bash?)没有关于如何做一个file.js的线索。这就是为什么unix的神发明了 shebang for:

whats making your current shell starting the bash is that your current shell (bash?) has no clue about what to do with a file.js. thats why the gods of unix invented the shebang for:


当文本文件第一行中的前两个字符出现时,由字符数字符号和感叹号(#!)组成的字符序列。在这种情况下,类Unix操作系统中的程序加载器解析第一行的其余部分作为解释器指令,并使用指定为参数的任何命令行选项调用字符序列后面指定的程序。
the character sequence consisting of the characters number sign and exclamation point (#!), when it occurs as the first two characters in the first line of a text file. In this case, the program loader in Unix-like operating systems parses the rest of the first line as an interpreter directive and invokes the program specified after the character sequence with any command line options specified as parameters.

所以,在你的情况下,我会尝试

so, in your case i would try to put

 #!/usr/bin/env node

您可以看到beeing应用于'inode'(交互式node.js)shell,这可能是另一个选项来激活脚本。

at the top of the script. you can see that beeing applied for example in the 'inode' (interactive node.js) shell, which might be another option to fire your scripts.

https://github.com/bancek/node-interactive-shell/blob/master/inode.js

这篇关于是否可以运行Node.js脚本而不调用`node`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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