打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
快速搭建 Node.js 开发环境以及加速 npm

快速搭建 Node.js 开发环境以及加速 npm 
在公交车上刷微博,还是有很多同学在咨询:

如何快速搭建 node 开发环境 
npm 超慢 
github 无法打开的问题 
于是我觉得应该写一篇文章解答所有这些起步问题,让新同学也能顺顺利利入门。

快速搭建 Node.js 开发环境

如果你想长期做 node 开发, 或者想快速更新 node 版本, 或者想快速切换 node 版本, 
那么在非 Windows(如 osx, linux) 环境下, 请使用 nvm 来安装你的 node 开发环境, 保持系统的干净.

如果你使用 Windows 做开发, 那么你可以使用 nvmw 来替代 nvm

osx, linux 环境

如果你是 windows 环境开发, 请跳过这里, 直接查看下一章.

git clone nvm

直接从 github clone nvm 到本地, 这里假设大家都使用 ~/git 目录存放 git 项目:

$ cd ~/git

$ git clone 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] Download and install a , [-s] from source 
nvm uninstall Uninstall a version 
nvm use Modify PATH to use 
nvm run [] Run with as arguments 
nvm current Display currently activated version 
nvm ls List installed versions 
nvm ls 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 [] Show all aliases beginning with 
nvm alias Set an alias named pointing to 
nvm unalias Deletes the alias named 
nvm copy-packages Install global NPM packages contained in 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=https://npm.taobao.org/mirrors/node nvm install 4 
于是你就会看到一段非常快速进度条:

################################################################## 100.0%

Now using node v4.3.2 
如果你不想每次都输入环境变量 NVM_NODEJS_ORG_MIRROR, 那么我建议你加入到 .bashrc 文件中:

nvm

export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node 
source ~/git/nvm/nvm.sh 
然后你可以继续非常方便地安装各个版本的 node 了, 你可以查看一下你当前已经安装的版本:

$ nvm ls 
nvm 
v0.8.26 
v0.10.26 
v0.11.11 
-> v4.3.2 
windows 环境

git clone nvmw

直接从 github clone nvmw 到本地, 这里假设大家都使用 d:\git 目录存放 git 项目:

 cd git 
$ git clone https://github.com/hakobera/nvmw.git 
设置 d:\git\nvmw 目录到你的 PATH 环境变量中:

set "PATH=d:\git\nvmw;%PATH%" 
重新打开你的终端, 输入 nvmw

$ nvmw

Usage: 
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 versions

Example: 
nvmw install v0.6.0 Install a specific version number 
nvmw use v0.6.0 Use the specific version 
通过 nvmw 安装任意版本的 node

nvmw 默认是从 http://nodejs.org/dist/ 下载的, 国外服务器, 必然很慢, 
好在 nvmw 以及支持从镜像服务器下载包, 于是我们可以方便地从七牛的 node dist 镜像下载:

$ set "NVMW_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node"

$ nvmw install 4.3.2 
于是你就会看到一段非常快速进度条:

################################################################## 100.0%

Now using node v4.3.2 
如果你不想每次都输入环境变量 NVMW_NODEJS_ORG_MIRROR, 那么我建议你在全局环境变量中增加它.

然后你可以继续非常方便地安装各个版本的 node 了, 你可以查看一下你当前已经安装的版本:

$ nvmw ls

v0.10.26 
v0.11.12 
Current: v4.3.2 
到此, 无论是 windows 环境, 还是 osx, linux 环境, 都能快速安装多个版本的 node 了.

使用 cnpm 加速 npm

同理 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 koa 
npm http GET https://registry.npm.taobao.org/koa 
npm http 200 https://registry.npm.taobao.org/koa 
... 
npm http 200 https://registry.npm.taobao.org/negotiator 
npm http 200 https://registry.npm.taobao.org/keygrip 
koa@0.5.2 node_modules/koa 
├── koa-compose@2.2.0 
├── statuses@1.0.2 
├── finished@1.1.1 
├── escape-html@1.0.1 
├── only@0.0.2 
├── debug@0.8.0 
├── fresh@0.2.2 
├── type-is@1.0.1 
├── delegates@0.0.3 
├── mime@1.2.11 
├── co@3.0.5 
├── accepts@1.0.1 (negotiator@0.4.2) 
└── cookies@0.4.0 (keygrip@1.0.0) 
但是毕竟镜像跟官方的 npm 源还是会有一个同步时间差异, 目前 cnpm 的默认同步时间间隔是 15 分钟. 
如果你是模块发布者, 或者你想马上同步一个模块, 那么推荐你安装 cnpm cli:

$ npm --registry=https://registry.npm.taobao.org install cnpm -g 
通过 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/mirrors/node install microtime 
再次要提到 cnpm cli, 它已经默认将 --registry 和 --disturl 都配置好了, 谁用谁知道 
写到这里, 就更快疑惑那些不想安装 cnpm cli 又吐槽 npm 慢的同学是基于什么考虑不在本地安装一个 cnpm 呢?

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用nvm安装node,全局npm,cnpm
正确的安装和使用nvm
NodeJS使用淘宝npm镜像站的各种姿势
需要装多个Node版本,怎么弄?用NVM
安装node
node-sass 安装的正确姿势
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服