Node.js在Sublime Text 2中构建系统 [英] Node.js build system in Sublime Text 2
问题描述
我开始学习JavaScript了。在这样做时,我厌倦了将我的JavaScript代码嵌入到HTML文档中,以便在浏览器中运行它。我认为在Sublime的控制台中运行我的脚本会很好,所以我不必离开编辑器。因此我试图创建一个JavaScript构建系统,因为Sublime没有一个。
I justed started learning JavaScript. While doing that, I got tired of embedding my JavaScript code into an HTML document in order to run it in the browser. I thought it would be nice to just run my scripts right in Sublime's console, so I wouldn't have to leave the editor. Therefore I was trying to create a JavaScript build system, since Sublime doesn't come with one.
我的想法是使用Node.js作为JavaScript解释器。我用Linux Mint的包管理器安装它。据我所知它运作得很好。假设我有一个包含以下JavaScript代码行的文件 test.js :
My idea was to use Node.js as the JavaScript interpreter. I installed it with the package manager of Linux Mint. As far as I can say it works just fine. Let's say I have a file test.js containing the following line of JavaScript code:
console.log("Hello World");
当我在控制台中运行
nodejs /path/to/test.js
时,我得到:
Hello World
但是,我不认为这适用于Sublime。我通过单击工具/构建系统/新构建系统创建了一个新的构建系统。然后我键入以下行:
However, I don't get this to work with Sublime. I created a new Build system by clicking Tools / Build System / New Build System. I then typed in the following lines:
{
"cmd": ["nodejs", "$file"]
}
据我所知,这一行是以下命令的JSON表示:
As far as I know, this one line is the JSON representation of the following command:
nodejs /path/to/current/file.ext
就像我说的,如果我在控制台中手动运行,它工作得很好。如果我在Sublime中按F7,这是 Build 的快捷方式,Sublime的控制台就会显示出来。它虽然是空的。
Like I said, if I run this manually in the console, it works just fine. If I press F7 in Sublime, which is the shortcut for Build, Sublime's console shows up. It's empty though.
还有一件奇怪的事情。即使Sublime控制台的(不存在的)输出指示构建系统未配置为正确使用Node.js,我在意外尝试运行非JS文件时显示了一些Node.js错误,例如 Node.sublime-build 文件。这是Sublime控制台中显示的输出:
There's another weird thing. Even though the (non-existing) output of Sublime's console indicates that the build system isn't configured to correctly work with Node.js, I got some Node.js errors displayed when I accidentally tried to run non-JS files such as the Node.sublime-build file. This is the output displayed in Sublime's console:
/home/baerenfaenger/.config/sublime-text-2/Packages/User/Node.sublime-build:2
"cmd": ["nodejs", "$file"]
^
module.js:434
var compiledWrapper = runInThisContext(wrapper,filename,真正);
^
语法错误:意外的令牌:
在Module._compile(module.js:434:25)
at Object..js(module.js:464:10)$在Module.load的b $ b(module.js:353:32)
在Function._load(module.js:311:12)
在Array.0(module.js:484:10)
在EventEmitter._tickCallback(node.js:190:39)
[以0.1秒完成退出代码1]
module.js:434 var compiledWrapper = runInThisContext(wrapper, filename, true); ^ SyntaxError: Unexpected token : at Module._compile (module.js:434:25) at Object..js (module.js:464:10) at Module.load (module.js:353:32) at Function._load (module.js:311:12) at Array.0 (module.js:484:10) at EventEmitter._tickCallback (node.js:190:39) [Finished in 0.1s with exit code 1]
为什么我没有得到任何输出在执行实际的JavaScript代码时?提前谢谢你!
So why do I not get any output when executing actual JavaScript code? Thank you in advance!
推荐答案
{
"cmd": ["node","$file"]
}
在Windows中完美适用于我显示sublime窗口中的输出。虽然我不知道如何在sublime之后停止服务器,但我必须搜索并终止节点进程。
works perfectly for me in windows and it shows the output in the sublime window. Although I dont know how to stop the server after from sublime, I have to search and kill the node process.
此外,check节点在你的PATH中;)
Also, check node is in your PATH ;)
希望它适合你。
这篇关于Node.js在Sublime Text 2中构建系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!