LoginSignup
85
84

More than 5 years have passed since last update.

Macにnvm + Node.jsをインストールする

Last updated at Posted at 2013-06-25

1. nvmインストール

下記コマンドを実行。

$ cd ~
$ git clone git://github.com/creationix/nvm.git .nvm
$ . .nvm/nvm.sh

.bash_profile.zshrcに下記を追記。

[[ -s (自分のホームディレクトリ)/.nvm/nvm.sh ]] && . (自分のホームディレクトリ)/.nvm/nvm.sh
nvm use default
npm_dir=${NVM_PATH}_modules
export NODE_PATH=$npm_dir

一旦ターミナルを抜けるとか、source ~/.bash_profileとかして、./bash_profileの更新を反映させる。

下記のようにnvmコマンドを実行して下記のように出力されたらOK。


Node Version Manager

Usage:
    nvm help                    Show this message
    nvm install [-s] <version>  Download and install a <version>
    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 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.4.12         Install a specific version number
    nvm use 0.2                 Use the latest available 0.2.x release
    nvm run 0.4.12 myApp.js     Run myApp.js using node v0.4.12
    nvm alias default 0.4       Auto use the latest installed v0.4.x version

2. Node.jsをインストール

下記コマンドを実行してインストールできるNode.jsのバージョンを確認。

$ nvm ls-remote

インストールしたいNode.jsのバージョンをコマンドラインオプションで指定して、Node.jsをインストール。

$ nvm install v0.10.12

package.jsonとかあれば下記コマンドで必要なライブラリをインストールすると、node_modulesDirectoryにpackage_jsonで指定されたライブラリがインストールされる。

$ npm install

3. Node.jsの実行

まずは、下記コマンドで使用するNode.jsのバージョンを指定する。

$ nvm use v0.10.12

package_jsonに下記のような記述があれば、

{
  "name": "hoge",
  "version": "0.0.1",
  "description": "hoge!",
  "main": "server.js",
  "scripts": {
    "test": "mocha test/**/*.test.js",
    "start": "node server.js"
  },

下記コマンドで server.jsが実行される。

$ npm start

もしくは下記コマンドで。

$ node server.js

4. nvm use **コマンドを実行するのが面倒な場合

$ nvm alias default v0.10.12

とかして、シェルを再起動したりするとデフォルトで使用するNode.jsのバージョンを指定できる。

85
84
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
85
84