前言
大家都知道,Sublime Text 安裝插件一般從 Package Control 中直接安裝即可,當我安裝 node js 插件時候,直接通過Package Control 安裝,雖然插件安裝成功了,但是找不到配置文件 Nodejs.sublime-build
來更改一些配置 。於是去 https://packagecontrol.io/packages/Nodejs 官網上查看,只提供一種安裝方式。
安裝
git安裝
-
MacOSX
git clone https://github.com/tanepiper/SublimeText-Nodejs.git ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/Nodejs
-
Windows
git clone https://github.com/tanepiper/SublimeText-Nodejs.git ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/Nodejs
-
Linux
git clone https://github.com/tanepiper/SublimeText-Nodejs $HOME/.config/sublime-text-3/Packages/Nodejs
手動安裝
通過地址https://github.com/tanepiper/SublimeText-Nodejs去github上下載該包,解壓放到Sublime Text3\Packages 目錄下。
修改配置文件 (兩處要修改)
Nodejs.sublime-settings
在 Sublie Text 3 Packages 文件目錄下, 找到 Nodejs.sublime-settings
文件,更改以下內容
修改后的文件
{
// 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"
"node_command": "C:\\Program Files\\nodejs\\node.exe" ,
// Same for NPM command
"npm_command": "C:\\Program Files\\nodejs\\npm.cmd",
// as 'NODE_PATH' environment variable for node runtime
"node_path": false,
"expert_mode": false,
"ouput_to_new_tab": false
}
注: 修改了兩個地方,分別是 node_command
和 npm_command
Nodejs.sublime-build
在 Sublie Text 3 Packages 文件目錄下, 找到 Nodejs.sublime-build
文件,更改以下內容
修改后的文件
"cmd": ["node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.js",
"shell":true,
"encoding": "utf8",
"windows":
{
"cmd": ["taskkill","/F", "/IM", "node.exe","&","node", "$file"]
},
"linux":
{
"cmd": ["killall node; node", "$file"]
},
"osx":
{
"cmd": ["killall node; node $file"]
}
}
注: 修改了兩個地方,分別是 encoding
和 windows 下的cmd
,windows 下的cmd命令是每次執行的時候都會kill
掉以前啟動的nodejs 進程,這個命令有些錯誤,我們修改它,到達我們想要的效果
測試
新建一個 test.js
文件 輸入以前內容
var http = require('http');
var os = require('os');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
Ctrl +B
編譯一下,會在Sublime Text 控制台 看到 Server running at http://127.0.0.1:3000/
,接下來我們從瀏覽器打開 訪問 http://127.0.0.1:3000/
.
結束語
以上就是 Sublime Text 排至 Node js 步驟 。
參考: https://packagecontrol.io/packages/Nodejs packagecontrol 官網 的Node js 插件.