LoginSignup
12
10

More than 5 years have passed since last update.

HomebrewでNode.jsをインストール+エラーの解決方法

Posted at

はじめに

MacにNode.jsをインストールしたときのメモです。

前提

Homebrewは事前にインストールしてください。
ターミナルのコマンドに記載している{user}はユーザのホームディレクトリです。

手順

1. nodebrewのインストール

以下のコマンドを実行してください。

$ brew install nodebrew

インストールできたかを確認するために、バージョン確認を行います。

$ nodebrew -v
nodebrew 0.9.7
...

と出てくればOKかと思います。

2. Node.jsのインストール

以下のコマンドを実行してください。(今回は最新版のインストールです)

$ nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v8.4.0/node-v8.4.0-darwin-x64.tar.gz
Warning: Failed to create the file 
Warning: /Users/{user}/.nodebrew/src/v8.4.0/node-v8.4.0-darwin-x64.tar.gz: No 
Warning: such file or directory

curl: (23) Failed writing body (0 != 941)
download failed: https://nodejs.org/dist/v8.4.0/node-v8.4.0-darwin-x64.tar.gz

私はこのようなエラーがでました。
ググってみると、インストール先のディレクトリがないのが原因だったので、
以下のコマンドでディレクトリを作成しました。

$ mkdir -p ~/.nodebrew/src

作成できたのでリトライ。

$ nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v8.4.0/node-v8.4.0-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully

インストールができました!
インストールできたかを確認するために、バージョン確認を行います。
(厳密にはローカルにインストールされたNode.jsのリストです)

$ nodebrew list
v8.4.0

current: none

上記の通り、「current: none」のままだとNodejsは実行できないので、
使用するバージョンをuseコマンドで有効化します。

$ nodebrew use v8.4.0
use v8.4.0

有効になったかをlistで確認。

$ nodebrew list
v8.4.0

current: v8.4.0

これで有効化されました。
次に、パスを通します。

$ echo 'export PATH=$PATH:/Users/{user}/.nodebrew/current/bin' >> ~/.bashrc

Node.jsのバージョン確認をします。

$ node -v
-bash: node: command not found

このような場合、
.bashrcの変更設定が反映されていないので、再反映します。

$ source ~/.bashrc

反映されたのでリトライ。

$ node -v
v8.4.0

npmも合わせて確認します。

$ npm -v
5.3.0

これで、Node.jsのインストールが完了しました!

12
10
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
12
10