LoginSignup
162
132

More than 5 years have passed since last update.

nodebrewでnodeのバージョンを切り替える方法

Last updated at Posted at 2018-04-25

nodeのバージョンを下げなければならなくなった経緯

laravelのwebpackを使ったコンパイルを実行する為npm run devを実行したら以下の様なエラー出てきました。

Module build failed: Error: Missing binding /your/path/hogehoge/node_modules/node-sass/vendor/darwin-x64-59/binding.node
Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 9.x

Found bindings for the following environments:
  - OS X 64-bit with Node.js 8.x

のnodeのバージョンを確認したところv9だったので多分v8に変更しろってことなんだろうなと思いました。

nodebrewの設定

homebrewがインストールされている前提で話をすすめます。homebrewがインストールされてない方はインストールして下さい。

今まで使っていたnodeをアンインストール

homebrewでnodeをインストールして使っていた場合
homebrewでインストールしたnodeを削除

brew uninstall --force node

nodebrewをインストール

homebrewでnodebrewをインストール

brew install nodebrew

nodebrewをインストールされたか確認

nodebrew

nodebrewで使えるコマンドが出てくればOK

nodebrewでインストール可能なnodeのバージョンを確認

nodebrew ls-remote

nodebrewで使いたいバージョンのnodeをインストール

nodebrew install-binary v8.11.1

今回はv8の一番最新のバージョンを選んだ。
他にも最新の安定版をインストールしたかったstableとする。

nodebrew install-binary stable

  nodeのインストール先のディレクトリを作る

インストールコマンドを実行したところエラーが出た。

Fetching: https://nodejs.org/dist/v8.11.1/node-v8.11.1-darwin-x64.tar.gz
Warning: Failed to create the file 
Warning: /your/path/.nodebrew/src/v8.11.1/node-v8.11.1-darwin-x64.tar.gz: 
Warning: No such file or directory
                                                                           0.0%
curl: (23) Failed writing body (0 != 847)
download failed: https://nodejs.org/dist/v8.11.1/node-v8.11.1-darwin-x64.tar.gz

No such file or directoryと言っているのでディレクトリを作った。

$ mkdir ~/.nodebrew
$ mkdir ~/.nodebrew/src

 そして再度nodeのインストールコマンドを実行したところうまくいった。
参考
https://blog.yug1224.com/archives/5693a5e0fef2539c7d5b149d

しかしよく調べたら以下のコマンドを実行することで自動でディレクトリを作ってくれるらしい。

nodebrew setup

なのでこちらで対応した方が良いと思います。
参考
http://ngzm.hateblo.jp/entry/2016/09/22/172500

nodebrewでインストールされたnodeのバージョンを確認

nodebrew ls

インストールしたバージョンのリストが表示されていればOK

v8.11.1
v9.11.1

current: none

currentのところには、使用するバージョンが表示されます。

使いたいnodeのバージョンをインストールしたバージョンのリストから選ぶ。

今回はv8.11.1を選ぶ。

nodebrew use v8.11.1

これでv8.11.1のnodeが使えると思ってnode -vを実行してみたが..

bash: /usr/local/bin/node: No such file or directory

パスの設定

~/.bashrcのファイルに以下パスを追加します。


export PATH=$HOME/.nodebrew/current/bin:$PATH

そしてパスを反映させるために以下のコマンドを実行
source ~/.bashrc

source ~/.bashrcの一文を~/.bash_profileに追加する

nodebrew setupを実行したら、パスが表示される。

Fetching nodebrew...
Installed nodebrew in $HOME/.nodebrew

========================================
Export a path to nodebrew:

export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================

参考サイト

補足

nodebrewと似たような機能を持つnvmというのもある。

162
132
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
162
132