Node.js をサーバに導入する際の、個人的な備忘録
概要
- クラウドの仮想サーバ上に Node.js をインストールする
- バージョンの切り替えや一覧表示とかしたいから nvm を使って入れたい
- そのため、nvm インストールしてから Node.js をインストールする
前提
- 対象サーバ: GCP (Google Cloud Platform) の AppEngine で立てた仮想サーバ
- OS: CentOS Linux release 7.5.1804 (Core)
- cURL がインストールされていること
nvm とは
nvm は "Node Version Manager" の略。
Node.js のバージョン管理ツール。
インストールやバージョン切り替えが簡単にできるやーつ。
nvm で Node.js をインストールすると、サーバ全体に対してではなく、インストールしたユーザに対して node 環境を構築する点に注意。
(インストールしていない他のユーザで node を実行しても動かない。実行パス通ってない)
手順
基本的には GitHub の公式ページ の手順に書いてあるとおり。
1.nvm をインストール
node を実行するユーザを用意して、ホームディレクトリに移動し、以下を実行。
# cURL で nvm のファイルをダウンロードし、インストール シェル実行
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
実行すると以下の結果となり .nvm
ディレクトリが出来あがる
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12819 100 12819 0 0 58922 0 --:--:-- --:--:-- --:--:-- 59073
=> Downloading nvm from git to '/home/ユーザ名/.nvm'
=> Cloning into '/home/ユーザ名/.nvm'...
remote: Enumerating objects: 267, done.
remote: Counting objects: 100% (267/267), done.
remote: Compressing objects: 100% (242/242), done.
remote: Total 267 (delta 31), reused 86 (delta 15), pack-reused 0
Receiving objects: 100% (267/267), 119.47 KiB | 0 bytes/s, done.
Resolving deltas: 100% (31/31), done.
=> Compressing and cleaning up git repository
=> Appending nvm source string to /home/ユーザ名/.bashrc
=> Appending bash_completion source string to /home/ユーザ名/.bashrc
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:
/usr/lib
├── @angular/cli@1.7.4
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:
$ nvm use system
$ npm uninstall -g a_module
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
終わったら source コマンド実行
source ~/.bashrc
nvm インストール確認
nvm バージョン確認。入った。
nvm --version
# コマンド実行結果
0.33.11
2.Node.js をインストール
インストール可能な一覧を確認する
nvm ls-remote
安定版の最新をインストール
nvm install stable
実行結果。入った。
Downloading and installing node v11.2.0...
Downloading https://nodejs.org/dist/v11.2.0/node-v11.2.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v11.2.0 (npm v6.4.1)
Creating default alias: default -> stable (-> v11.2.0)
バージョン指定してインストールする場合 (Optional)
nvm install v0.10 # 末尾にバージョンを直接指定
Node.js バージョン確認
node -v
# コマンド実行結果
v11.2.0
ついでに npm バージョン確認
npm は "Node Package Manager" の略。Node 環境で使用できるモジュールやライブラリを管理・インストールなどできる便利なツール。今後の node アプリ開発でたくさん使う事になる。
npm -v
# コマンド実行結果
6.4.1
デフォルトで使用する Node.js のバージョンを指定しておく
1つの場合は自動的にそれが選択されるが、複数の Node.js バージョンをインストールし、切り替えてからログアウトし直すと、nvm 管理の Node.js は選択されず、システムの Node.js となる。
なので、デフォルトで使用するバージョンを指定しておく。
# デフォルト指定
nvm alias default stable
# source コマンド
source ~/.bashrc
おまけ
既にインストールされている Node.js のバージョンを確認
nvm ls
何も入ってない時の実行結果
-> system
node -> stable (-> N/A) (default)
iojs -> N/A (default)
lts/* -> lts/dubnium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.14.4 (-> N/A)
lts/carbon -> v8.12.0 (-> N/A)
lts/dubnium -> v10.13.0 (-> N/A)
1つインストールした後の実行結果
-> v11.2.0
system
default -> stable (-> v11.2.0)
node -> stable (-> v11.2.0) (default)
stable -> 11.2 (-> v11.2.0) (default)
iojs -> N/A (default)
lts/* -> lts/dubnium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.14.4 (-> N/A)
lts/carbon -> v8.12.0 (-> N/A)
lts/dubnium -> v10.13.0 (-> N/A)