LoginSignup
133
134

More than 5 years have passed since last update.

centos + node.js + npm + nvm インストール

Last updated at Posted at 2015-05-22

0527 追記

とりあえずサクッとnodejsをインストールの場合は以下(centos 6)
$ sudo yum install epel-release
$ sudo yum install nodejs
$ node -v
ついでにnpmをインストールするのであれば
$ yum install -y npm --enablerepo=epel

概要

nodejsのバージョン管理 nvm をまず、インストールし
その後、nvmを使ってnodejsをインストールする

1. nvm インストール

nvmとは、node.js のバージョンを切り替え容易にできるツール

▼ nvmをインストール
$ git clone git://github.com/creationix/nvm.git ~/.nvm
$ source ~/.nvm/nvm.sh

▼nvm コマンドが実行できることの確認
$ nvm help

2. nvmを使ってnode.jsをインストール

▼インストール可能なnodeのバージョンリストを一覧表示
$ nvm ls-remote

▼インストール
$ nvm install 0.10.38

▼インストールされたことの確認
$ node -v

3. nvm設定

▼ デフォルトのnodeのバージョンを設定
$ nvm alias default v0.10.26

▼ ターミナル起動時にnvmコマンドが実行できるように設定
vi ~/.bash_profile

if [[ -s ~/.nvm/nvm.sh ]];
 then source ~/.nvm/nvm.sh
fi

4. node.js サンプルプログラム

▼helloworld.jsを作る
$ vi helloworld.js

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

▼実行
$ node helloworld.js

▼ブラウザに以下URLを貼り付けて「Hello World」と出たら成功です
http://127.0.0.1:1337/

good luck!

133
134
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
133
134