首先来讲,Mac上可以有几种安装nodejs开发环境的方法,然而各种坑我都踩过了,还是建议rvm来安装nodejs,事实上好像我现在并没有遇到什么古怪的问题了,所以这篇文章其实并不能解决初学者的所有问题,顶多只是把一种最佳流程给记录下来罢了。

首先还是上脚本,适用于Ubuntu,Bash环境,同时也适用于Mac的Terminal/iTerm2环境(但需要一点点更改,主要是去掉apt-get相关的指令):

#!/bin/bash

echo_title () {
  echo $*
}
install_packages () {
  if [[ $OSTYPE == *darwin* ]]; then
    brew install $*
  else
      declare -a RR
      for pkg in $*; do
        dpkg --get-selections|grep -Pi "$pkg[ \t]+install" 1>/dev/null
        [[ $? -eq 0 ]] && (echo "    $pkg exists";) || RR=("${RR[@]}" "$pkg")
      done
      [[ ${#RR[@]} > 0 ]] && (echo "  将要安装 ${RR[@]}..."; apt-get install -y --force-yes ${RR[@]} && return 0) || return 1;
  fi
}

install-nvm-nodejs () {
	echo_title "install_nvm_and_nodejs"
	
	#NODE_VERSION="5.7.1"
	#NODE_VERSION="5.10.1"
	#NODE_VERSION="5.11.1"
	#NODE_VERSION="6.1.0"
	NODE_VERSION="6.2.0"

        if [[ $OSTYPE == *darwin* ]]; then
          xcode-select --install
        else
	  install_packages build-essential libssl-dev
        fi
	
	pushd $HOME

	[ ! -d ~/.nvm ] && (curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh ;  source ~/.bashrc)
	
	#source $HOME/.bashrc
	export NVM_DIR="$HOME/.nvm"
	[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
	
	#nvm ls-remote
	nvm install v0.11.16
	#nvm install v0.12.9
	nvm install v0.12.10
	#nvm install iojs-v3.3.1
	#nvm install v5.3.0
	nvm install v$NODE_VERSION
	nvm use v0.12.10
	nvm alias default v$NODE_VERSION
	nvm use default
	grep 'nvm use default' ~/.bashrc || echo "nvm use default" >> ~/.bashrc
	
	# npm fix:
	# npm install -g cnpm --registry=https://registry.npm.taobao.org
	# or:
	# alias cnpm="npm --registry=https://registry.npm.taobao.org \
	# --cache=$HOME/.npm/.cache/cnpm \
	# --disturl=https://npm.taobao.org/dist \
	# --userconfig=$HOME/.cnpmrc"
	#
	# # Or alias it in .bashrc or .zshrc
	# $ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npm.taobao.org \
	#   --cache=$HOME/.npm/.cache/cnpm \
	#   --disturl=https://npm.taobao.org/dist \
	#   --userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc
	grep 'registry=https://registry.npm.taobao.org' ~/.npmrc || (
		npm config set registry https://registry.npm.taobao.org
		npm cache clean
	)
	npm config set unsafe-perm true
	npm info underscore
	npm install -g npm
	npm install -g bower grunt-cli gulp yo generator-webapp
	npm install -g jshint js-beautify ycssmin
	npm install -g nodemon express express-generator socket.io
	npm install -g grunt-karma karma karma-phantomjs-launcher karma-jasmine jasmine-core phantomjs-prebuilt
	npm install -g react-native-cli

	popd
}

nodejs-versions () {
	nvm
	echo "node: $(node -v)"
	echo "npm: $(npm -v)"
	echo "bower: $(bower -v)"
	echo "grunt: $(grunt --version)"
	echo "gulp: $(gulp --version|head -n1)"
	echo "yo: $(yo --version)"
	echo "express: $(express --version)"
}


install-nvm-nodejs && nodejs-versions
exit 0;

rvm管理nodejs版本是非常简单的。你可以用 rvm install v6.1.0 && rvm alias default v6.1.0 && rvm use default 这样的序列来安装和设置自己的工作环境nodejs主力版本。