LoginSignup
4
3

More than 1 year has passed since last update.

MacにNodeをインストール、バージョンを切り替える方法とNodeのバージョンについて

Posted at

Macにnodeをインストール、バージョンを切り替える方法について備忘録としてまとめます。

実行環境:

Mac OS ver.12.1
M1チップ

インストール方法

nodebrew を使用してインストールします。
homebrew を利用するため、インストールしてない場合はこちらからインストール。

nodebrew のインストール

brew install nodebrew

nodeのインストール

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

LTS versionのインストール。

nodebrew install-binary latest

安定版のインストール

(2022/05時点ではv18.1.0)

nodebrew install-binary stable

現在提供されているバージョンの確認

nodebrew ls-remote

バージョンを指定してインストール

ls-remoteに存在するバージョンを指定。

nodebrew install-binary v16.13.0

インストールに失敗した場合

以下のようなエラーが発生。

Password:
Fetching: https://nodejs.org/dist/v18.1.0/node-v18.1.0-darwin-x64.tar.gz
Warning: Failed to create the file 
Warning: [HOME]/.nodebrew/src/v18.1.0/node-v18.1.0-darwin-x64.tar.gz: No 
Warning: such file or directory
curl: (23) Failure writing output to destination

download failed: https://nodejs.org/dist/v18.1.0/node-v18.1.0-darwin-x64.tar.gz

おそらくnodebrew インストール時の初回のみ発生する。
nodebrew のセットアップを行い、再度インストール。

nodebrew setup

nodebrewの適用

現在ローカルのnodebrewで管理(?)しているバージョンの確認

nodebrew ls

バージョンの切り替え

nodebrew use v16.13.0

nodebrew の適用

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

確認

適用したバージョンが表示されればOK!

node --version

Nodeのバージョンについて

今回、nodeを使用したパッケージをインストールして、npm startでlocalhostにサーバーを立てることを行った。

パッケージはnpmを使用してインストールした。このパッケージは最新版(LTS)のNodeを要求していたため、それに従い最新版のNodeにて実行。

npm startでサーバーを起動しようとしたところ、エラーが発生。
どうもMacではnpm run dev-serverとした方がよいとのこと。

しかしnpm run dev-serverで実行すると以下のエラー(一部抜粋)が発生。

Browserslist: caniuse-lite is outdated. Please run:
  npx browserslist@latest --update-db
  Why you should do it regularly: https://github.com/browserslist/browserslist#browsers-data-updating
(node:5681) [DEP_WEBPACK_DEV_SERVER_HTTPS] DeprecationWarning: 'https' option is deprecated. Please use the 'server' option.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:5681) [DEP_WEBPACK_DEV_SERVER_CACERT] DeprecationWarning: The 'cacert' option is deprecated. Please use the 'ca' option.
node:internal/crypto/hash:67
  this[kHandle] = new _Hash(algorithm, xofLen);
                  ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:67:19)
    at Object.createHash (node:crypto:133:10)

sudoで実行したりパッケージやNodeを再インストール、検証したりと色々試したが、最終的な解決策としては、Nodeのバージョンを安定版よりも落として、うまくいった。

最終的な解決方法

# インストールと切り替え
nodebrew install-binary v16.13.0
nodebrew use v16.13.0
# 適用
echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile
# nodeのversionの確認
node --version

node って、古いバージョンじゃないと動かないとかってよくあるんですかね?
僕は詳しくないので、詳しい方いたらぜひ教えていただけると幸いです。

参考

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