---
title: "Sublime + NodeJs 开发环境"
author: "weistar"
date: "2014-12-21T20:51:03Z"
tags: []
categories: ["Web前端"]
slug: "sublime-nodejs"
draft: true
lastmod: "2014-12-21T20:58:18Z"
id: 2169044
url: "https://weistar.iteye.com/blog/2169044"
---

1\. 从www.nodejs.org下载[nodejs](http://blog.csdn.net/fan158/article/details/17484403)并安装到指定的目录。

 

2.下载sublime并安装,下载列表：[http://www.sublimetext.com/2](http://www.sublimetext.com/2)

 

3.下载sublime的nodejs插件，需是集成Nodejs插件到sublime中，下载地址：[https://github.com/tanepiper/SublimeText-Nodejs](https://github.com/tanepiper/SublimeText-Nodejs)

 

4.解压zip文件， 并重命名文件夹“Nodejs”

 

5.打开sublime，操作"preference" --&gt; "Browse packages", 打开一个目录，这个目录是众多语言IDE插件的存放地。

 

6.复制”Nodejs“文件夹到这个目录。

 

7.打开Nodejs文件夹，找到文件“Nodejs.sublime-build”， 拖拽到sublime，显示：

 

```
{  
  "cmd": ["node", "$file"],  
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",  
  "selector": "source.js",  
  "shell":true,  
  "encoding": "cp1252",  
  "windows":  
    {  
        "cmd": ["taskkill /F /IM node.exe & node", "$file"]  
    },  
  "linux":  
    {  
        "cmd": ["killall node; node", "$file"]  
    }  
}  
```

 需要更改上面标注的地方， 要更改encoding为GB2312或者utf8，如果不更改此属性可能导致build nodejs代码时终端显示乱码。

 

 

8.要用sublime打开文件“Nodejs.sublime-settings” 或者 设置“preference” --》 “package settings” --》 “Nodejs”  --》“setting-default” 打开文件并 更改成：

 

```
{  
  // save before running commands  
  "save_first": true,  
  // if present, use this command instead of plain "node"  
  // e.g. "/usr/bin/node" or "C:\bin\node.exe"  
  // e.g. "/usr/bin/node" or "C:\bin\node.exe"  
  "node_command": "D:\\ProgramFiles\nodejs\node.exe",  //是Nodejs安装目录  
  // Same for NPM command  
  "npm_command": "D:\\ProgramFiles\nodejs\npm.cmd",   
  // as 'NODE_PATH' environment variable for node runtime  
  //"node_path": false,  
  
  "expert_mode": false,  
  
  "ouput_to_new_tab": false  
}  
```

 

 

9.验证，复制下面代码 到sublime一个新建文件中。 并Ctrl+ b 编译这段代码。 即时窗口显示：Server running at http://127.0.0.1:80/  ，然后打开浏览器敲入这个地址并显示内容hello nodejs  。 这是一个简单的站点发布。

 

```
var http = require('http');   
var os = require('os');     
  
http.createServer(function (request, response) {     
  response.writeHead(200, {'Content-Type': 'text/plain'});     
  response.end('Hello nodejs\n');     
  
}).listen(80);     
  
console.log('Server running at http://127.0.0.1:80/');  
```

 

 

10.**在 Sublime 直接运行 Javascript 调试控制台**

有个问题，那就是Sublime Text不支持一些语言的运行，例如javascript，我们通常通过写一个HTML页面来嵌入JS，然后再用Chrome或者Firefox的开发者工具来调试。而实际上我们也可以通过添加Build System的方法来让Sublime Text支持Javascript Console控制台调试。

 

10.1.**利用 JSC 添加 JavaScript 控制台 (Mac OS X)**

实际上Mac内置了一个javascript控制台程序，但是藏得太深，它在

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc

里。实际上你可以在终端里直接运行这个程序，然后在终端进行调试。

 

为Sublime Text添加此Build System，只需打开Sublime Text，选择 Tools &gt; Build System &gt; New Build System... ，如图所示

![](http://dl2.iteye.com/upload/attachment/0104/5298/35ca3738-7e8e-3362-af9f-7bbd260cf06a.png)  
 

 现在，粘贴以下内容并保存为JSC.sublime-build，或者其他名字：

 

```
{
"cmd": ["/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc", "$file"],
"selector": "source.js"
}
```

 

 现在，在 Tools &gt; Build System 里选择刚刚创建好的“JSC”，就可以调试了。

你需要做的就是在写好js文件后，按下Ctrl + B按钮。 

![](http://dl2.iteye.com/upload/attachment/0104/5304/c5a3b731-de44-3462-88bd-37047966e934.png)  
 

不过这里也要注意，使用JSC时，你需要用debug()替换console.log()函数。

 

 10.2 **利用 Node.js 添加 JavaScript 控制台**

除了Mac的JSC，我们还有强大的Node.js。要使用此方案，请先下载并安装Node.js。

其他的步骤和上述基本一致，只是在编辑新的Build System时，要粘贴的内容是：

```
{
"cmd": ["node", "$file"],
"selector": "source.js"
}
```

 编辑好后保存为Node.sublime-build或者其他的名字就可以了。

 

同样，要使用，在 Tools &gt; Build System 里选择刚刚创建好的“Node”，就可以调试了。

你需要做的就是在写好js文件后，按下Command + B按钮。

在Node.js的情况下，你可以继续使用console.log()函数。