【初心者向け】zsh: command not found: npm がでた時の対処法

目次

準備①:エラーの意味を確認

npmなんてコマンドは見つからないよ!
(インストールしないと使えないよ!)

準備②:作業環境の共有

PC:MacBook Pro
チップ:Apple M2 Pro
OS:Ventura

準備③:どうすればエラーが出なくなるの?

node.jsをインストールすればnpmが使える!

node.jsをインストールすればエラーが解消されるよ!

準備④:npmを使うための手順を確認

  1. Homebrewのインストール(nodebrewをインストールするために必要)
  2. nodebrewのインストール(Node.jsのバージョン管理&インストールに必要)
  3. node.jsのインストール
  4. パスを通す

node.jsをインストールするために、やることがたくさんある。。。
早速やってみよう!!!

手順①: Homebrewのインストール

Homebrewがインストールされているか確認する

Bash
brew -v
Bash
Homebrew 4.2.8

↑こんな感じで表示されたらインストールされています!
※数字部分はバージョンなのでインストールタイミングによって異なります

もし「command not found: brew」と出たら
Homebrewがインストールされていないよ!

インストールされていなかったら

インストールされていないことを確認したらこちらのコマンドを打ってインストールしましょう

Bash
 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

※2024年2月21日時点のものなので、念の為下記URLからコピペした方が無難ですhttps://brew.sh/ja/

こんなエラーが出ることも

Bash
==> Downloading https://ghcr.io/v2/homebrew/portable-ruby/portable-ruby/blobs/sha256:d783cbeb6e6ef0d71c0b442317b54554370decd6fac66bf2d4938c07a63f67be
##################################################################################################################################################################### 100.0%
==> Pouring portable-ruby-3.1.4.arm64_big_sur.bottle.tar.gz
Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/username/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

その場合はこのエラー文にも書いてある通り下記二つのコマンドを実行しましょう!

Bash
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/username/.zprofile
Bash
eval "$(/opt/homebrew/bin/brew shellenv)"

「username」の部分は人によって異なります!!

手順②: nodebrewのインストール

先ほどインストールしたHomebrewを使って、今度はnodebrewをインストールします。

nodebrewがインストールされているか確認する

下記コマンドで確認しましょう
きちんとインストールできていれば、バージョンが表示されます

Bash
nodebrew -v

インストールされていなかったら

Bash
brew install nodebrew

これでnodebrewがインストールできます。

ちなみに、このコマンドを打つといろんな文字がターミナル上に出現してちょっと焦ります笑

Bash
==> Downloading https://formulae.brew.sh/api/formula.jws.json
##################################################################################################################################### 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
##################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/nodebrew/manifests/1.2.0
##################################################################################################################################### 100.0%
==> Fetching nodebrew
==> Downloading https://ghcr.io/v2/homebrew/core/nodebrew/blobs/sha256:eed2aeff4fd05a4c2969d670ce9a38bc01832ac90b65a1c773689532cb376660
##################################################################################################################################### 100.0%
==> Pouring nodebrew--1.2.0.all.bottle.tar.gz
==> 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

To use Homebrew's directories rather than ~/.nodebrew add to your profile:
  export NODEBREW_ROOT=/opt/homebrew/var/nodebrew

zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions
==> Summary
🍺  /opt/homebrew/Cellar/nodebrew/1.2.0: 8 files, 40.6KB
==> Running `brew cleanup nodebrew`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

この「Add path:」という部分が大切になりますがそれは後述します

ここまでで「nodebrew」がインストールされただけで、
「node.js」が使えるようにはなっていません

手順③: node.jsをインストール

下記コマンドで、インストールできるnode.jsのバージョンを確認できます
めっちゃ出てくるので焦らないように笑

Bash
nodebrew ls-remote

確認できたら、インストールしましょう

Bash
nodebrew install v21.6.1 ※バージョンは各自指定してください

インストールできたら、useで実行しましょう

Bash
nodebrew use v21.6.1

手順④: パスを通す

下記二つのコマンドを実行してください

zshの場合

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

bashの場合

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

これでパスが通るよ!!

node.jsがインストールされているか確認する

こちらのコマンドを打って、バージョンが表示されるか確認しましょう。

Bash
node -v

ちなみにnode.jsをインストールと同時に、npmもインストールされます

下記のコマンドを打ってバージョンが表示されてればOK

Bash
npm -v

まとめ

この記事でエラー解決できたら幸いです。
これからも一緒に頑張りましょう!!

あなたの挑戦を応援しています!!

よかったらシェアしてね!
目次