3
1

More than 1 year has passed since last update.

M1 MacにNode.jsをインストールする方法と使い方(Node.jsバージョン管理)

Posted at

はじめに

nodenvを使うと、Node.jsのバージョン管理がしやすくなります。つまり、プロジェクト毎にNodeのバージョンを指定して実行ができます。

今回はM1 MACにHomebrewを使ったnodenvのインストール方法と使い方をこの記事でまとめていきたいと思います。

環境

・macOS Big Sur 11.4

1.Homebrewをインストール

Homebrewをインストールしていない場合は、先にHomebrewをインストールします。

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

2.nodenvをインストール

nodenvをインストールします。

$ brew install nodenv

3.nodenv にパスを通す

自分は以下の記述がなかったため上手くバージョン管理ができなかったです。

export PATH="$HOME/.nodenv/bin:$PATH"
eval "$(nodenv init -)"

nodenvの設定が完了しているかどうかを確認します。以下のような結果になればOKです。

$ curl -fsSL https://github.com/nodenv/nodenv-installer/raw/master/bin/nodenv-doctor | bash
Checking for `nodenv' in PATH: /usr/local/bin/nodenv
Checking for nodenv shims in PATH: OK
Checking `nodenv install' support: /usr/local/bin/nodenv-install (node-build 4.9.43)
Counting installed Node versions: 1 versions
Auditing installed plugins: OK

ここまで設定できればターミナルを閉じて、再度開くとnodenvが使えるようになります。

指定のバージョンのNode.jsをインストールする

推奨版はNode.jsで常に更新されているため確認が必要です。

# インストール可能なNode.jsのバージョン一覧
$ nodenv install -l
...
16.13.2
16.14.0
16.14.1
16.14.2
16.15.0
...

# Node.jsの16.14.1と16.15.0をインストールする
$ nodenv install 16.14.1
$ nodenv install 16.15.0

# nodenvに認識させる
$ nodenv rehash

Node.jsのバージョンを切り替える

# インストールされているNode.jsのバージョン一覧を確認
$ nodenv versions
  16.14.1
    16.15.0

# ローカル(カレントディレクトリ配下)で利用するNode.jsのバージョンを設定する
$ nodenv local 16.14.1

# グローバル(システム全体)で利用するNode.jsのバージョンを設定する
$ nodenv global 16.14.1
3
1
0

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
3
1