在公交车上刷微博,仍是有不少同窗在咨询:html
因而我以为应该写一篇文章解答全部这些起步问题,让新同窗也能顺顺利利入门。github
若是你想长期作 node 开发, 或者想快速更新 node 版本, 或者想快速切换 node 版本, 那么在非 Windows(如 osx, linux) 环境下, 请使用 nvm 来安装你的 node 开发环境, 保持系统的干净.web
若是你使用 Windows 作开发, 那么你可使用 nvmw 来替代 nvmshell
若是你是 windows 环境开发, 请跳过这里, 直接查看下一章.npm
直接从 github clone nvm 到本地, 这里假设你们都使用 ~/git
目录存放 git 项目:ubuntu
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | NVM_DIR="/mnt/f/ubuntu/nvm" bash
git clone https://github.com/creationix/nvm.git /mnt/f/ubuntu/nvm && cd /mnt/f/ubuntu/nvm && git checkout 'git describe --abbrev=0 --tags'
配置终端启动时自动执行 source ~/git/nvm/nvm.sh
, 在 ~/.bashrc
, ~/.bash_profile
, ~/.profile
, 或者 ~/.zshrc
文件尾部添加如下命令:windows
vi /etc/profile
export NVM_DIR="/mnt/f/ubuntu/nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
source /etc/profile
从新打开你的终端, 输入 nvm
$ nvmNode Version ManagerUsage: 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 versionExample: 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 shellNote: to remove, delete or uninstall nvm - just remove ~/.nvm, ~/.npm and ~/.bower folders
nvm 默认是从 http://nodejs.org/dist/ 下载的, 国外服务器, 必然很慢, 好在 nvm 以及支持从镜像服务器下载包, 因而咱们能够方便地从七牛的 node dist 镜像下载:
$ NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/dist nvm install 0.11.11
因而你就会看到一段很是快速进度条:
######################################################################## 100.0%Now using node v0.11.11
若是你不想每次都输入环境变量 NVM_NODEJS_ORG_MIRROR
, 那么我建议你加入到 .bashrc
文件中:
# nvmexport NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/distsource ~/git/nvm/nvm.sh
而后你能够继续很是方便地安装各个版本的 node 了, 你能够查看一下你当前已经安装的版本:
$ nvm ls nvm v0.8.26 v0.10.26 v0.11.11-> v0.11.12
安装 npm
curl -L https://www.npmjs.com/install.sh | sh
直接从 github clone nvmw 到本地, 这里假设你们都使用 d:\git
目录存放 git 项目:
$ d:$ cd git $ git clone https://github.com/hakobera/nvmw.git
设置 d:\git\nvmw
目录到你的 PATH
环境变量中:
set "PATH=d:\git\nvmw;%PATH%"
从新打开你的终端, 输入 nvmw
$ nvmwUsage: nvmw help Show this message nvmw install [version] Download and install a [version] nvmw uninstall [version] Uninstall a [version] nvmw use [version] Modify PATH to use [version] nvmw ls List installed versionsExample: nvmw install v0.6.0 Install a specific version number nvmw use v0.6.0 Use the specific version
nvmw 默认是从 http://nodejs.org/dist/ 下载的, 国外服务器, 必然很慢, 好在 nvmw 以及支持从镜像服务器下载包, 因而咱们能够方便地从七牛的 node dist 镜像下载:
$ set "NVMW_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node" $ nvmw install 0.11.11
因而你就会看到一段很是快速进度条:
######################################################################## 100.0%Now using node v0.11.11
若是你不想每次都输入环境变量 NVMW_NODEJS_ORG_MIRROR
, 那么我建议你在全局环境变量中增长它.
而后你能够继续很是方便地安装各个版本的 node 了, 你能够查看一下你当前已经安装的版本:
$ nvmw ls v0.10.26v0.11.12Current: v0.11.12
到此, 不管是 windows 环境, 仍是 osx, linux 环境, 都能快速安装多个版本的 node 了.
同理 nvm , npm 默认是从国外的源获取和下载包信息, 不慢才奇怪. 能够经过简单的 ---registry
参数, 使用国内的镜像 https://registry.npm.taobao.org :
$ npm --registry=https://registry.npm.taobao.org install koa
因而屏幕又哗啦哗啦地一大片输出:
$ npm --registry=https://registry.npm.taobao.org install koanpm http GET https://registry.npm.taobao.org/koanpm http 200 https://registry.npm.taobao.org/koa...npm http 200 https://registry.npm.taobao.org/negotiatornpm http 200 https://registry.npm.taobao.org/keygripkoa[@0](/user/0).5.2 node_modules/koa├── koa-compose[@2](/user/2).2.0├── statuses[@1](/user/1).0.2├── finished[@1](/user/1).1.1├── escape-html[@1](/user/1).0.1├── only[@0](/user/0).0.2├── debug[@0](/user/0).8.0├── fresh[@0](/user/0).2.2├── type-is[@1](/user/1).0.1├── delegates[@0](/user/0).0.3├── mime[@1](/user/1).2.11├── co[@3](/user/3).0.5├── accepts[@1](/user/1).0.1 (negotiator[@0](/user/0).4.2)└── cookies[@0](/user/0).4.0 (keygrip[@1](/user/1).0.0)
可是毕竟镜像跟官方的 npm 源仍是会有一个同步时间差别, 目前 cnpm 的默认同步时间间隔是 15 分钟. 若是你是模块发布者, 或者你想立刻同步一个模块, 那么推荐你安装 cnpm cli:
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
经过 cnpm 命令行, 你能够快速同步任意模块:
$ cnpm sync koa connect mocha
呃, 我就是不想安装 cnpm cli 怎么办? 哈哈, 早就想到你会这么懒了, 因而咱们还有一个 web 页面:
例如我想立刻同步 koa, 直接打开浏览器: https://npm.taobao.org/sync/koa
或者你是命令行控, 经过 open 命令打开:
$ open https://npm.taobao.org/sync/koa
若是你安装的模块依赖了 C++ 模块, 须要编译, 确定会经过 node-gyp 来编译,node-gyp 在第一次编译的时候, 须要依赖 node 源代码, 因而又会去 node dist 下载, 因而你们又会吐槽, 怎么 npm 安装这么慢…
好吧, 因而又要提到 --disturl
参数, 经过淘宝的镜像来下载:
$ npm --registry=https://registry.npm.taobao.org --disturl=https://npm.taobao.org/dist install microtime
再次要提到 cnpm cli, 它已经默认将 --registry
和 --disturl
都配置好了, 谁用谁知道 . 写到这里, 就更快疑惑那些不想安装 cnpm cli 又吐槽 npm 慢的同窗是基于什么考虑不在本地安装一个 cnpm 呢?
好了, 看到这里你们应该对 node 和 npm 已经没有速度慢的问题了.
github 慢, 或者说是它的资源 host 被堵而已, 你们能够经过简单的 hosts 映射解决:
185.31.16.184 github.global.ssl.fastly.net
据说是 APCN2 又断了: http://weibo.com/1415338244/ACTYkq8xK (APCN2 以修复好久)