Javascript Sublime Text 2 中的 Node.js 构建系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14427520/
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
Node.js build system in Sublime Text 2
提问by lambdarookie
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.
我刚开始学习 JavaScript。这样做时,我厌倦了将 JavaScript 代码嵌入到 HTML 文档中以便在浏览器中运行。我认为在 Sublime 的控制台中直接运行我的脚本会很好,这样我就不必离开编辑器。因此我试图创建一个 JavaScript 构建系统,因为 Sublime 没有自带。
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.jscontaining the following line of JavaScript code:
我的想法是使用 Node.js 作为 JavaScript 解释器。我用 Linux Mint 的包管理器安装了它。据我所知,它工作得很好。假设我有一个包含以下 JavaScript 代码行的文件test.js:
console.log("Hello World");When I run
当我跑
nodejs /path/to/test.js在我的控制台中,我得到:
Hello World但是,我无法将其与 Sublime 一起使用。我通过单击创建了一个新的构建系统Tools / Build System / New Build System工具/构建系统/新构建系统。然后我输入了以下几行:
{
"cmd": ["nodejs", "$file"]
} 据我所知,这一行是以下命令的 JSON 表示: nodejs /path/to/current/file.ext就像我说的,如果我在控制台中手动运行它,它工作得很好。如果我在 Sublime 中按 F7,这是BuildBuild,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-buildfile. This is the output displayed in Sublime's console:
还有一件很奇怪的事。尽管 Sublime 控制台的(不存在的)输出表明构建系统未配置为与 Node.js 一起正确工作,但当我不小心尝试运行非 JS 文件(例如Node.sublime-build文件。这是 Sublime 控制台中显示的输出:
/home/baerenfaenger/.config/sublime-text-2/Packages/User/Node.sublime-build:2
"cmd": ["nodejs", "$file"]
^
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 代码时没有得到任何输出呢?先感谢您!回答by garcianavalon
{
"cmd": ["node","$file"]
}
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.
在 Windows 中非常适合我,它在 sublime 窗口中显示输出。虽然我不知道如何在 sublime 之后停止服务器,但我必须搜索并杀死节点进程。
Also, check node is in your PATH ;)
此外,检查节点是否在您的 PATH 中;)
Hope it works for you.
希望对你有效。
回答by oozzal
Mac users, type which nodein terminal. It outputs the path of the node executable, which in most cases is /usr/local/bin/node. Now create a new build system (Tools > Build System > New Build System) with the following settings in Sublime Text.
Mac 用户,输入which node终端。它输出节点可执行文件的路径,在大多数情况下是/usr/local/bin/node. 现在Tools > Build System > New Build System在 Sublime Text 中使用以下设置创建一个新的构建系统 ( )。
{
"cmd": ["/usr/local/bin/node", "$file"],
"selector": "source.js"
}
回答by Luke W
Tested on Ubuntu Linux. Use a file selector as below:
在 Ubuntu Linux 上测试。使用文件选择器,如下所示:
{
"cmd": ["node", "$file"],
"selector": "*.js"
}
回答by Soman Dubey
Here is a tip for Windows,
这是 Windows 的提示,
Most of the answers from this thread is correct but none is complete and at least no answer tells about how to kill existing Node process even if you are able to run node in sublime. So eventually if either of above approach worked for you, You will end up manually killing Node always.
该线程中的大多数答案都是正确的,但没有一个是完整的,即使您能够在 sublime 中运行 node,至少也没有任何答案说明如何终止现有的 Node 进程。因此,最终如果上述任何一种方法对您有用,您最终都会手动杀死 Node。
Here is What I have done for 'Windows':
这是我为“Windows”所做的:
- Open Sublime Text, Goto "Tools" -> "Build System" -> "New Build System"
- Above will open a new file, paste below content there, and save it as nodejs.xxx ( use default extension): { "cmd": ["c:/test.bat","$file"] }
- Create a file ( c:/test.bat ) and paste this content (both of 2 lines) there:
- 打开 Sublime Text,转到“工具”->“构建系统”->“新建构建系统”
- 以上将打开一个新文件,粘贴下面的内容,并将其保存为 nodejs.xxx(使用默认扩展名): { "cmd": ["c:/test.bat","$file"] }
- 创建一个文件( c:/test.bat )并将此内容(两行)粘贴到那里:
taskkill /F /IM "node.exe" "c:/Program Files/nodejs/node.exe" %1
taskkill /F /IM "node.exe" "c:/Program Files/nodejs/node.exe" %1
- Save it, Switch to Sublime Text and select "Tools" -> "Build System" -> "nodejs"
- open any Nodejs Exectuable file and Hit CTRL+B.
- You're good to go. :)
- 保存,切换到 Sublime Text 并选择“工具”->“构建系统”->“nodejs”
- 打开任何 Nodejs 可执行文件并按 CTRL+B。
- 你很高兴去。:)
Enjoy noding.
喜欢点头。
回答by rsc
The method #2 of the link below solved for me:
下面链接的方法#2 为我解决了:
http://www.wikihow.com/Create-a-Javascript-Console-in-Sublime-Text
http://www.wikihow.com/Create-a-Javascript-Console-in-Sublime-Text
回答by Darth Pabor
This woks for me in Windows7
这在 Windows7 中对我有用
1) Open Sublime Text, Goto "Tools" -> "Build System" -> "New Build System"
1) 打开 Sublime Text,转到“工具”->“构建系统”->“新建构建系统”
2) Paste this text
2) 粘贴此文本
{
{
"cmd": ["C:\Program Files (x86)\nodejs\node","$file"]
}
}
\\ Escaping character
\\ 转义字符
3) Save as Node.sublime-build
3)另存为Node.sublime-build
4) Write a new js file, and press CTRL+B for build.
4) 新建一个js文件,按CTRL+B进行build。
Click herefor see image example.
单击此处查看图像示例。
回答by Gustavo Fontinha
I'm using windows and I created a New Build System with this content:
我正在使用 Windows,并使用以下内容创建了一个新的构建系统:
{
"cmd": ["node", "$file"],
"selector": "source.js",
"working_dir": "${project_path:${folder}}",
"path": "C:\Program Files\nodejs"
}
And after a ran a JavaScript file with sucessul inside Sublime.
然后在 Sublime 中运行一个带有 sucessul 的 JavaScript 文件。
My JavaScript file has this content:
我的 JavaScript 文件包含以下内容:
var a = 10;
var b = 20;
var resultado = a + b;
console.log('resultado:', resultado);
回答by Krzysztof Wende
Normally
一般
"cmd":["node",$file]
would work. However there is no way to stop the server than. I've found out the best solution which opens server in new console and the closes it automatically. I also works fine with supervisors or node-inspector
会工作。但是没有办法停止服务器比。我找到了在新控制台中打开服务器并自动关闭它的最佳解决方案。我也可以与主管或节点检查员一起工作
"cmd" : ["start", "cmd.exe", "/C", "node", "$file"]
I hope It'll help
我希望它会有所帮助
回答by phani
I can only see the console output only if the file exists/saved on disk. (for anonymous file I don't see the output either)
只有当文件存在/保存在磁盘上时,我才能看到控制台输出。(对于匿名文件,我也看不到输出)
Hope this helps
希望这可以帮助
回答by henok_al
On Windows, the following configuration works:
在 Windows 上,以下配置有效:
{
"cmd": ["C:\Program Files\nodejs\node.exe", "$file"],
"selector": "source.js"
}

