• 如何快速搭建 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] <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=https://npm.taobao.org/dist nvm install 0.11.11

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

######################################################################## 100.0%
Now using node v0.11.11

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

# nvm
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/dist
source ~/git/nvm/nvm.sh

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

$ nvm ls
nvm
v0.8.26
v0.10.26
v0.11.11
-> v0.11.12

windows 环境

git clone nvmw

直接从 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

$ 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/dist"
$ nvmw install 0.11.11

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

######################################################################## 100.0%
Now using node v0.11.11

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

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

$ nvmw ls

v0.10.26
v0.11.12
Current: v0.11.12

到此, 无论是 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](/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 --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/dist install microtime

再次要提到 cnpm cli, 它已经默认将 --registry--disturl 都配置好了, 谁用谁知道 . 写到这里, 就更快疑惑那些不想安装 cnpm cli 又吐槽 npm 慢的同学是基于什么考虑不在本地安装一个 cnpm 呢?

github 好慢

好了, 看到这里大家应该对 node 和 npm 已经没有速度慢的问题了.

github 慢, 或者说是它的资源 host 被堵而已, 大家可以通过简单的 hosts 映射解决:

185.31.16.184 github.global.ssl.fastly.net

为何最近访问国外网站这么慢?

听说是 APCN2 又断了: http://weibo.com/1415338244/ACTYkq8xK (APCN2 以修复很久)

有爱

快速搭建 Node.js 开发环境以及加速 npm的更多相关文章

  1. 【转】使用nvm快速搭建 Node.js 开发环境

    原文链接:http://www.cnblogs.com/shuoer/p/7802891.html 快速搭建 Node.js 开发环境 如果你想长期做 node 开发, 或者想快速更新 node 版本 ...

  2. 手把手教你学node之搭建node.js开发环境

    搭建node.js开发环境 本文只针对在Linux或者Mac下面.至于使用 Windows 并坚持玩新技术的同学,我坚信他们一定有着过人的.甚至是不可告人的兼容性 bug 处理能力,所以这部分同学麻烦 ...

  3. ES6 学习笔记 (2)-- Liunx环境安装Node.js 与 搭建 Node.js 开发环境

    笔记参考来源:廖雪峰老师的javascript全栈教程 一.安装Node.js 目前Node.js的最新版本是6.2.x.首先,从Node.js官网下载对应平台的安装程序. 1.下载 选择对应的Liu ...

  4. 1.0搭建 Node.js 开发环境

    <搭建 Node.js 开发环境> 本课程假设大家都是在 Linux 或者 Mac 下面.至于使用 Windows 并坚持玩新技术的同学,我坚信他们一定有着过人的.甚至是不可告人的兼容性 ...

  5. MongoDB 搭建Node.js开发环境

    理解Mongoose Elegant MongoDB object modeling for Node.js   安装Mongoose   $ cnpm install --save mongoose ...

  6. Oracle 搭建Node.js开发环境

      先决条件 安装oralce客户端驱动. 安装node.js.   创建项目 安装oracledb模块 $npm install oracledb 如果失败了,你可能要爬墙.   参考package ...

  7. DevExtreme 搭建Node.js开发环境

    简介 DevExtreme is a component suite for creating highly responsive web applications for touch devices ...

  8. Linux虚拟机中 Node.js 开发环境搭建

    Node.js 开发环境搭建: 1.下载CentOS镜像文件和VMWare虚拟机程序; 2.安装VMWare——>添加虚拟机——>选择CentOS镜像文件即可默认安装带有桌面的Linux虚 ...

  9. [转载]Sublime Text 3 搭建 React.js 开发环境

    [转载]Sublime Text 3 搭建 React.js 开发环境 Sublime有很强的自定义功能,插件库很庞大,针对新语言插件更新很快,配合使用可以快速搭建适配语言的开发环境. 1. babe ...

随机推荐

  1. HDU 3555 Bomb 数位DP 入门

    给出n,问所有[0,n]区间内的数中,不含有49的数的个数 数位dp,记忆化搜索 dfs(int pos,bool pre,bool flag,bool e) pos:当前要枚举的位置 pre:当前要 ...

  2. [Mysql] MySQL配置文件my.cnf的理解

    一.缘由 最近要接手数据库的维护工作,公司首选MySQL.对于MySQL的理解,我认为很多性能优化工作.主从主主复制都是在调整参数,来适应不同时期不同数量级的数据. 故,理解透彻my.cnf里的参数是 ...

  3. 建立dblink

    源地址:http://blog.itpub.net/24104981/viewspace-1116085/ create database link dblinkname connect to use ...

  4. C++学习15 继承权限和继承方式

    C++继承的一般语法为: class 派生类名:[继承方式] 基类名{ 派生类新增加的成员 }; 继承方式限定了基类成员在派生类中的访问权限,包括 public(公有的).private(私有的)和 ...

  5. JAVA 中关于String的特性

    一.初始化String的两种方式 String str1 = "hello"; String str2 = new String("hello"); 第一种方式 ...

  6. Codeforces 665D Simple Subset [简单数学]

    题意: 给你n个数,让你从中选一个子集要求子集中的任何两个数相加都是质数. 思路: 一开始把自己坑了,各种想,后来发现一个简单的性质,那就是两个数相加的必要条件是这两个数之中必定一个奇数一个偶数,(除 ...

  7. [POJ 2063] Investment (动态规划)

    题目链接:http://poj.org/problem?id=2063 题意:银行每年提供d种债券,每种债券需要付出p[i]块钱,然后一年的收入是v[i],到期后我们把本金+收入取出来作为下一年度本金 ...

  8. Machine Schedule(最小覆盖)

    其实也是个最小覆盖问题 关于最小覆盖http://blog.csdn.net/u014665013/article/details/49870029 Description As we all kno ...

  9. activity退出

    这里介绍两种方法:一种把每个activity记住,然后逐一干掉:另一种思路是使用广播.本文来源于网络,如有雷同,那是必须的.写此贴只是为了总结一下常用东东,还望原作者莫怪,本人真不是想侵权. 方法一. ...

  10. java cmd 命令

    java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后关闭命令窗口. cmd /k dir 是执行完d ...