LoginSignup
3
0

More than 1 year has passed since last update.

【M1Mac】nodebrewを使ってNode.jsをインストール

Last updated at Posted at 2022-05-18

はじめに

はじめてNode.jsをインストールするとなったときにパスが通らなくて手間取ってしまったので記事を作りました。

Homebrewをインストールする

  1. Homebrewのサイトのインストール欄に書かれているコマンドをコピーしてターミナル上で実行

  2. 処理が終わった後に、最後らへんの行に指示が書かれているのでそれに従って実行する

  3. Homebrewが正常に使えることを確認

    % brew -v
    % brew doctor
    Your system is ready to brew.
    

nodebrewをインストールする

  1. Homebrewを使ってインストール

    % brew install nodebrew
    
  2. 以下の行を~/.zshrcまたは~/.zprofileに追記する

    • 多分初期状態のままだと、~/.zshrcは存在してなくて、~/.zprofileは存在しているはず
    • ~/.zprofileには既にHomebrewのインストール時に追加された行が存在しているはず。その下にコピペ
    export PATH=$HOME/.nodebrew/current/bin:$PATH
    

    zshrcとzprofileについて - gallardo diary

  3. 変更を反映する

    % source  ~/.zprofile  # または ~/.zshrc
    
  4. 処理が終わったら、最後らへんの行に以下のような指示が書かれているのでそれに従って実行する

    Caveats
    You need to manually run setup_dirs to create directories required by nodebrew:
      /opt/homebrew/opt/nodebrew/bin/nodebrew setup_dirs
    
    Add path:
      export PATH=$HOME/.nodebrew/current/bin:$PATH
    
  5. nodebrewコマンドが使えるようになったことを確認
    まだNode.js自体はインストールしていないので以下のような表示がされると思います。

    % nodebrew ls
    not installed
    
    current: none
    

Node.jsをインストール

Stable版をインストール

% nodebrew install stable
% nodebrew ls
v18.2.0

current: none

最新版をインストール

% nodebrew install latest
% nodebrew ls
v18.2.0

current: none

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

v16.0.0以上の場合

% nodebrew ls-remote  // 指定可能なバージョンの一覧を表示
% nodebrew install v18.2.0
% nodebrew ls
v18.2.0

current: none

v16.0.0未満の場合

M1macのようなarm64に対応していないバージョンのNode.jsはそのままだとインストールできないです。以下のようになってしまいます。

% nodebrew install v14.8.0
v14.8.0 is not found

Can not fetch: https://nodejs.org/dist/v14.8.0/node-v14.8.0-darwin-arm64.tar.gz

なのでcompileを使ってインストールする必要があります。ちなみにインストールには30分~1時間ほどかかります。

% nodebrew compile v14.8.0

Node.jsにパスを通す

  1. nodebrewのcurrentに任意のバージョンのNode.jsを指定する
    パスはcurrentに通しているので、currentに何らかのNode.jsを指定しないとnodeコマンドが使えません。

    % nodebrew use v18.2.0
    % nodebrew use stable
    % nodebrew use latest
    
  2. nodeコマンドとnpmコマンドが使えることを確認

    % node -v
    % npm -v
    

おわりに

うまくいかない点があればコメントで教えてください🙇‍♂️

参考

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