久々にWeb開発をやってるのですが、いつの間にかnpmコマンドが通らなくなっていることに気がつきました。
zsh: command not found: npm
↑の参考記事によると
mojaveまではデフォルトシェルがbashだったのですが、catalinaからはzshがデフォルトに変わるみたいです。
ということでMacOSのアップデートに起因するものらしい。
そうなんだ……とっくにBigSurにアップデートしていた私。
長らくWeb開発の現場を離れていたので気が付きませんでした。
解決方法
以下のステップを順に行います。
1
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
% /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
Warning: The Ruby Homebrew installer is now deprecated and has been rewritten in
Bash. Please migrate to the following command:
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”==> Checking for `sudo` access (which may request your password).
Password: ここにMacのパスワードを入力==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/HomebrewPress RETURN to continue or any other key to abort
↑何らかのキーを押して続行
2
brew install nodebrew
🍺 /usr/local/Cellar/nodebrew/1.1.0: 8 files, 39KB
↑ビールマークが出たら成功。
一応 nodebrew -v
を叩いてインストールできてるか確認しておきましょう。
3
nodebrew install stable
nodebrewの安定版をインストール。
インストールするバージョンはお好みでOKで、例えば
nodebrew install latest
=> 最新版をインストールnodebrew install v〇〇.XX.□
=> 指定したバージョンをインストールnodebrew ls-remote
=> インストール可能なバージョンを一覧表示
この辺のコマンドが役に立ちそうです。
インストール実行後、
nodebrew ls
で該当のバージョンが表示されているのを確認し、
nodebrew use v16.11.1
(該当のバージョン番号を入力) とすると、currentが該当バージョンに切り替わります。
4
echo "export PATH=$HOME/.nodebrew/current/bin:$PATH" >> ~/.zprofile
従来は .bash_profile にパスを通していましたが、catalina以降は .zprofile にパスを通す必要があるそうです。
5
ターミナルを再起動後 node -v
を実行し、該当バージョンが確認できればOK。
npm -v
などで確認すると無事に npmコマンドが実行できるようになりました。