LoginSignup
336
323

nvm(Node Version Manager)を使ってNode.jsをインストールする手順

Last updated at Posted at 2017-12-22

概要

  • Node.jsの開発環境を構築するにあたって、nodebrew、nvmのようなバージョン管理できるライブラリを使うことが必須になっていると思います。
  • 個人的には、nvmがオススメなのでをnvmを使ったNode.jsのインストール&バージョンアップ手順を紹介します。

nodebrew、nvmのどちらを選択した方が良いか

結論:人気度からnvmがオススメ

  • Googleトレンド(2021年4月27日時点)で、nvmnodebrewを圧倒的に上回っている。

スクリーンショット 2021-04-27 20.34.22.png

記事の前提

インストール

curlでのインストール
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

* Homebrewでのインストール方法を掲載しているブログもありますが、現在、Homebrewでのインストールは公式でサポートしてないのでHomebrewでのインストールはしない方が良いです。

以下、https://github.com/nvm-sh/nvm より引用

Homebrew installation is not supported. 
If you have issues with homebrew-installed nvm, 
please brew uninstall it, and install it using the instructions below, 
before filing an issue.

nvmのインストール検証

次に正常にインストールされているか検証します。

nvmと表示されたらOKです。

検証コマンド
command -v nvm

もしここで、nvm: command not foundと表示されたら、以下コマンドを実行して、もう一度、command -vを実行してみてください

bashの場合
source ~/.bash_profile
zshの場合
source ~/.zshrc

Node.jsのインストール

stableバージョンを使う場合

stableバージョン
nvm install stable --latest-npm
nvm alias default stable
  • stableは、Node.jsの安定したバージョンをinstallするというオプション。
  • alias defaultで、安定したバージョンをdefaultにします。

LTS(Long Term Support)バージョンを使う場合

LTSとは長期間に渡って、安全なメンテナンスリリースのみが実施されるバージョンです。
最新バージョンを使う理由が特に無ければ、安定しているLTSを使うのがオススメです。
LTSのリリースは以下、6つの修正に限定されます。

  1. Bug fixes;
  2. Security updates;
  3. Non-semver-major npm updates;
  4. Relevant documentation updates;
  5. Certain performance improvements where the risk of breaking existing applications is minimal;
  6. Changes that introduce large amount of code churn where the risk of breaking existing applications is low and where the change in question may significantly ease the ability to backport future changes due to the reduction in diff noise.
  • LTSなどのリリース計画について詳しく知りたい方はこちらを参考にしてください。
  • リリーススケジュールを知りたい方はこちらを参考にしてください。
LTSバージョン
nvm install --lts --latest-npm
nvm alias default 'lts/*'
  • --ltsは、LTSバージョンの最新をinstallするというオプションです。

Node.jsのサンプルコード

インストールが正常に完了していることを検証します。
以下、web_example.jsというファイルを用意して実行します

web_example.js

const http = require("http");
  
http.createServer((request, response) => {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);
$ node web_example.js

以下、URLにアクセスしてみてください
http://localhost:8888/

Hello Worldと表示されたら検証完了です。

Node.jsのバージョンアップ手順

インストール手順と同様のコマンドでOK

Node.jsのバージョンアップ
nvm install --lts
nvm alias default 'lts/*'

npm自体のバージョンアップも実施します。

nvm install-latest-npm

動作検証として一時的にstableバージョンにしたいような場合には、以下のようにuseを使います。

nvm install stable --latest-npm
nvm use stable

特定のバージョンにしたい場合は、nvm ls-remoteで利用可能なversionを検索してから、そのバージョンをインストール(nvm install バージョン番号)。バージョン指定(nvm alias default バージョン番号)します。

例:v18.9.1に変更する場合
nvm install v18.9.1
nvm alias default v18.9.1

プロジェクトで固定のNode.jsのバージョンを使わせたい場合

  • プロジェクトによっては、Node.jsのバージョンを固定したい場合があると思います。
  • そのような場合、.nvmrcファイルを作成してソース管理すれば対応できるので紹介します。
  • 以下、最新のltsで固定する場合の例です。
最新のltsで固定する場合
echo "lts/*" > .nvmrc
nvm install # 初回のみ実行する
nvm use

nvm本体のバージョンアップ方法

以下コマンドをコピペして、ターミナルで実行します。

コマンド
(
  cd "$NVM_DIR"
  git fetch --tags origin
  git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
) && \. "$NVM_DIR/nvm.sh"
実行結果
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 1 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 990 bytes | 123.00 KiB/s, done.
From https://github.com/nvm-sh/nvm
   ef3b20c..a82edf4  master     -> origin/master
HEAD is now at 9600617 v0.39.1

その他参考となる記事

[宣伝] GAS入門の教材 - Udemy

  • 2023年11月時点で、約3,000人の受講生を獲得し、UdemyBusinessにも選定されてます
  • もしGASに興味があれば、以下のバナーからUdemyにて購入ご検討ください

336
323
4

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
336
323