树莓派之 Node.js 环境配置

一键安装

优点:简单、快捷、方便、省事。

缺点:以这个方法安装的Node.js版本比较低。

1
$ sudo aptitude install nodejs

源码安装

优点:能安装最新版或任意版本的Node.js。

缺点:源码编译时间的时间比较长。

1
2
3
4
5
6
$ wget http://nodejs.org/dist/node-latest.tar.gz
$ tar -xvzf node-latest.tar.gz
$ cd node-latest/
$ ./configure
$ make
$ sudo make install

软件包安装

优点:简单、快捷、方便、省事,可以安装到比较新的版本。

缺点:暂未发现。

1
2
$ wget http://node-arm.herokuapp.com/node_latest_armhf.deb
$ sudo dpkg -i node_latest_armhf.deb

通过NVM安装

优点:官网v0.10.28之前的任意版本都可以直接安装;可以任意切换不同版本;

缺点:v0.10.28之后的版本都要以源码编译的方式安装。

  1. 安装NVM:

    1
    $ git clone https://github.com/creationix/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
  2. 编辑 ~/.bashrc~/.profile~/.zshrc~/.bash_profile 文件(如果存在的话),添加下面代码到最后一行:

    1
    2
    3
    4
    5
    6
    if [ -f ~/.nvm/nvm.sh ]; then
    export NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node
    export NVM_IOJS_ORG_MIRROR=http://npm.taobao.org/mirrors/iojs
    . ~/.nvm/nvm.sh
    [[ -r ~/.nvm/bash_completion ]] && . ~/.nvm/bash_completion
    fi
  3. 通过以下类似命令安装任意版本的Node.js,以v0.10.28为例:

    1
    $ nvm install v0.10.28
  4. 切换或使用特定版本的Node.js:

    1
    $ nvm use v0.10.27
  5. 查看使用的Node.js版本:

    1
    $ nvm ls

通过n模块更新Node.js

  1. 安装n模块:

    1
    $ sudo npm install -g n
  2. 更新最新版Node.js:

    1
    $ sudo n latest

安装CNPM

  1. 具体参考:淘宝 NPM 镜像

    1
    $ npm install -g cnpm --registry=http://registry.npm.taobao.org
  2. 通过CNPM安装模块

    1
    $ cnpm install [name]

解决NPM慢的问题

  1. 使用淘宝NPM镜像:

    1
    $ sudo npm config set registry http://registry.npm.taobao.org/
  2. 使用代理:

    1
    2
    $ sudo npm config set proxy http://hx.gy:1080
    $ sudo npm config set https-proxy http://hx.gy:1080
  3. 查看配置:

    1
    $ sudo npm config list

安装io.js

io.js的安装相对比较简单,官方已有编译好的二进制文件下载。

访问io.js官网,选择相应的系统平台安装包,然后输入以下命令(这个方法卸载的时候比较麻烦):

1
2
3
4
5
6
7
8
9
$ cd ~
$ wget https://iojs.org/dist/latest/iojs-v3.3.0-linux-armv7l.tar.gz
$ tar -xvzf iojs-v3.3.0-linux-armv7l.tar.gz
$ cd iojs-v3.3.0-linux-armv7l/
$ rm -f ./*.md
$ rm -f ./LICENSE
$ sudo cp -rf --remove-destination ./* /usr/local/
$ cd ..
$ rm -rf iojs-v3.3.0-linux-armv7l/