61
57

More than 3 years have passed since last update.

Mac > node.jsをインストール

Last updated at Posted at 2017-10-11

いろんなサイトを見て試してみたがうまくいかず、
以下の設定ではうまくいったのでメモ

環境

2017/10/11
macOS Sierra (10.12.6)

homebrew インストール

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

nodebrew インストール

nodebrewの最新版をインストール

brew install nodebrew
nodebrew -v

nodeの最新版をインストール

最初に保存するフォルダを作成、その後インストール

mkdir -p ~/.nodebrew/src
nodebrew install-binary latest

nodeのバージョンを指定して入れたい場合には以下

$ nodebrew ls-remote

バージョン一覧が表示されるので、任意のバージョン番号を以下の要領で

$ nodebrew install-binary v11.15.0

nodeを有効にする。(vを選択する)

$ nodebrew list

バージョン一覧が表示されるので、任意のバージョン番号を以下の要領で指示する。(セットする)

$ nodebrew use vX.X.X

パスを通す

いろんな説明サイトでこの部分が違っており、うまくいかなかったが、
以下の設定でうまくいった。

zshの場合

echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.zshrc

変更の適用

exec $SHELL -l

確認

node --version

bashの場合

echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile

bash_profileの編集

viで、~/.bash_profileを編集

vi ~/.bash_profile

oキーで行を追加し入力モードになる。
以下を追加

if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi

:wq! コマンドで保存

編集後は以下のようになっているはず

export PATH=$HOME/.nodebrew/current/bin:$PATH
if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi
~        

nodeの確認

sourceコマンドで変更を反映させる

source ~/.bash_profile
node -v

でバージョンが表示されればOK

参考

61
57
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
61
57