標籤:gis 0.11 切換 進度條 folders 伺服器 glob ejs 3.0
快速搭建 Node.js 開發環境以及加速 npm
- 如何快速搭建 node 開發環境
- npm 超慢
- github 無法開啟的問題
於是我覺得應該寫一篇文章解答所有這些起步問題,讓新同學也能順順利利入門。
快速搭建 Node.js 開發環境
如果你想長期做 node 開發, 或者想快速更新 node 版本, 或者想快速切換 node 版本,
那麼請使用 nvm 來安裝你的 node 開發環境, 保持系統的乾淨.
git clone nvm
直接從 github clone nvm 到本地, 這裡假設大家都使用 ~/git 目錄存放 git 項目:
$ cd ~/git $ git clone [https://github.com/creationix/nvm.git](https://github.com/creationix/nvm.git)
配置終端啟動時自動執行 source ~/git/nvm/nvm.sh,
在 ~/.bashrc, ~/.bash_profile, ~/.profile, 或者 ~/.zshrc 檔案添加以下命令:
source ~/git/nvm/nvm.sh
重新開啟你的終端, 輸入 nvm
$ nvm Node Version Manager Usage: nvm help Show this message nvm --version Print out the latest released version of nvm nvm install [-s] <version> Download and install a <version>, [-s] from source nvm uninstall <version> Uninstall a version nvm use <version> Modify PATH to use <version> nvm run <version> [<args>] Run <version> with <args> as arguments nvm current Display currently activated version nvm ls List installed versions nvm ls <version> List versions matching a given description nvm ls-remote List remote versions available for install nvm deactivate Undo effects of NVM on current shell nvm alias [<pattern>] Show all aliases beginning with <pattern> nvm alias <name> <version> Set an alias named <name> pointing to <version> nvm unalias <name> Deletes the alias named <name> nvm copy-packages <version> Install global NPM packages contained in <version> to current version Example: nvm install v0.10.24 Install a specific version number nvm use 0.10 Use the latest available 0.10.x release nvm run 0.10.24 myApp.js Run myApp.js using node v0.10.24 nvm alias default 0.10.24 Set default node version on a shell Note: to remove, delete or uninstall nvm - just remove ~/.nvm, ~/.npm and ~/.bower folders
通過 nvm 安裝任意版本的 node
nvm 預設是從 http://nodejs.org/dist/ 下載的, 國外伺服器, 必然很慢,
好在 nvm 以及支援從鏡像伺服器下載包, 於是我們可以方便地從七牛的 node dist 鏡像下載:
$ NVM_NODEJS_ORG_MIRROR=[http://dist.u.qiniudn.com](http://dist.u.qiniudn.com/) nvm install 0.11.11
於是你就會看到一段非常快速進度條:
###### ################################################################## 100.0%Now using node v0.11.11
如果你不想每次都輸入環境變數 NVM_NODEJS_ORG_MIRROR, 那麼我建議你加入到 .bashrc 檔案中:
# nvmexport NVM_NODEJS_ORG_MIRROR=[http://dist.u.qiniudn.com](http://dist.u.qiniudn.com/) source ~/git/nvm/nvm.sh
然後你可以繼續非常方便地安裝各個版本的 node 了, 你可以查看一下你當前已經安裝的版本:
$ nvm ls nvm v0.8.26 v0.10.26 v0.11.11 -> v0.11.12
使用 cnpm 加速 npm
同理 nvm , npm 預設是從國外的源擷取和下載包資訊, 不慢才奇怪.
可以通過簡單的 ---registry 參數, 使用國內的鏡像 http://r.cnpmjs.org :
$ npm --registry=[http://r.cnpmjs.org](http://r.cnpmjs.org/) install koa
於是螢幕又嘩啦嘩啦地一大片輸出:
$ npm --registry=[http://r.cnpmjs.org](http://r.cnpmjs.org/) install koa npm http GET [http://r.cnpmjs.org/koa](http://r.cnpmjs.org/koa) npm http 200 [http://r.cnpmjs.org/koa](http://r.cnpmjs.org/koa) npm http GET [http://r.cnpmjs.org/koa/download/koa-0.5.2.tgz](http://r.cnpmjs.org/koa/download/koa-0.5.2.tgz) npm http 200 [http://r.cnpmjs.org/koa/download/koa-0.5.2.tgz](http://r.cnpmjs.org/koa/download/koa-0.5.2.tgz) npm http GET [http://r.cnpmjs.org/escape-html](http://r.cnpmjs.org/escape-html) npm http GET [http://r.cnpmjs.org/statuses](http://r.cnpmjs.org/statuses) ... npm http 200 [http://r.cnpmjs.org/negotiator](http://r.cnpmjs.org/negotiator) npm http 200 [http://r.cnpmjs.org/keygrip](http://r.cnpmjs.org/keygrip) [[email protected]](mailto:[email protected]) node_modules/koa ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ├── [[email protected]](mailto:[email protected]) ([[email protected]](mailto:[email protected])) └── [[email protected]](mailto:[email protected]) ([[email protected]](mailto:[email protected]))
但是畢竟鏡像跟官方的 npm 源還是會有一個同步時間差異, 目前 cnpm 的預設同步時間間隔是 15 分鐘.
如果你是模組發行者, 或者你想馬上同步一個模組, 那麼推薦你安裝 cnpm cli:
$ npm --registry=[http://r.cnpmjs.org](http://r.cnpmjs.org/) install cnpm -g
通過 cnpm 命令列, 你可以快速同步任意模組:
$ cnpm sync koa connect mocha
呃, 我就是不想安裝 cnpm cli 怎麼辦? 哈哈, 早就想到你會這麼懶了, 於是我們還有一個 web 頁面:
例如我想馬上同步 koa, 直接開啟瀏覽器: http://cnpmjs.org/sync/koa
或者你是命令列控, 通過 open 命令開啟:
$ open [http://cnpmjs.org/sync/koa](http://cnpmjs.org/sync/koa)
如果你安裝的模組依賴了 C++ 模組, 需要編譯, 肯定會通過 node-gyp 來編譯,
node-gyp 在第一次編譯的時候, 需要依賴 node 原始碼, 於是又會去 node dist 下載,
於是大家又會吐槽, 怎麼 npm 安裝這麼慢...
好吧, 於是又要提到 --disturl 參數, 通過七牛的鏡像來下載:
$ npm --registry=[http://r.cnpmjs.org](http://r.cnpmjs.org/) --disturl=[http://dist.u.qiniudn.com](http://dist.u.qiniudn.com/) install microtime
再次要提到 cnpm cli, 它已經預設將 --registry 和 --disturl 都配置好了, 誰用誰知道
.
快速搭建 Node.js 開發環境以及加速 npm