0
0

brewでNode.jsを導入後、npmが使えなくなった

Posted at

brew installでnodeの導入。

これでnpmも付随してインストールされるはず。

brew install node
==> 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/node/manifests/21.2.0
Already downloaded: /Users/hoge/Library/Caches/Homebrew/downloads/cb041cbb6422d2540d408235089e8612368dacca144ce930e65adf38129fda67--node-21.2.0.bottle_manifest.json
==> Fetching node
==> Downloading https://ghcr.io/v2/homebrew/core/node/blobs/sha256:23aa79a37e3d2957eee9634c52347ceb2ef514b2f24616b8f56570a4497c6a06
Already downloaded: /Users/hoge/Library/Caches/Homebrew/downloads/75e39f43d3998c4a2879733f323023ee1a4450bcf28df29ea5d2b04557332c69--node--21.2.0.sonoma.bottle.tar.gz
==> Pouring node--21.2.0.sonoma.bottle.tar.gz
Warning: The post-install step did not complete successfully
You can try again using:
  brew postinstall node
==> Summary
🍺  /usr/local/Cellar/node/21.2.0: 2,203 files, 64.3MB
==> Running `brew cleanup node`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

Sunnaryを確認

==> Summary
🍺  /usr/local/Cellar/node/21.2.0: 2,203 files, 64.3MB

インストールが成功して/usr/local/Cellar/node/21.2.0にnode関連のディレクトリが作成された。

nodeのバージョン確認

$node -v
v21.2.0

nodeはインストールされており、PATHも通っている。

npmのバージョン確認

$npm -v
zsh: command not found: npm

インストールされていない?
OR
PATHが通っていない?

npm調査

下記のいずれかが原因と考えて調査。

  • インストールされていない
  • PATHが通っていない

インストールされていない?

nodeがインストールされたディレクトリにnpm関連のファイルが存在するかを確認。

$find /usr/local/Cellar/node/21.2.0 -name "npm*"

/usr/local/Cellar/node/21.2.0/libexec/bin/npm
/usr/local/Cellar/node/21.2.0/libexec/lib/node_modules/npm
.
.
.

npmは存在するようです。

インストールされたnpmのバージョンを確認する。

$/usr/local/Cellar/node/21.2.0/libexec/bin/npm -v

10.2.3

ちゃんと10.2.3のnpmがインストールされています。一安心。
原因は、PATHが通っていないことでした。

npmにPATHを通す

以下コマンド実行して、でシンボリック・リンクを作成

$sudo ln -s /usr/local/Cellar/node/21.2.0/libexec/bin/npm /usr/local/bin/npm

npmコマンドが使えることを確認

$npm -v
10.2.3

なぜ、npmのPATHが通らなかったのか?

brewでnodeをインストールをした際に、npmのシンボリックリンクも自動で作成されるはずなのですが、今回作成されませんでした。

原因

原因の特定には至りませんでしたが、以前brewを使用せずにnode.jsをインストールしたことがあり、それをアンインストールした際にnpm関連のファイルを消し忘れていたのが原因かもしれないです。該当ファイルについては、今回の事象について調査する過程で削除しました。
下記コマンドを使用して、npm関連のゴミファイルがないか確認しました。

$sudo find / -type f -name npm

下記に試したことを記載しています。

試したこと

brewでnodeの再インストール

brewでnodeの再インストールを試しましたが、うまくいきませんでした。

$brew uninstall node
$brew install node

brewの状態を確認してerrorの原因がないか確認

特に問題はなさそう。

$brew doctor

Your system is ready to brew.

brew install node時に警告が発生していた

brew install node実行時の出力を確認すると、以下の警告文を吐いていた。

Warning: The post-install step did not complete successfully
You can try again using:
  brew postinstall node

再度、brew postinstall nodeを実行するもうまくいかず、これについてはよくわからなかったです。

$brew postinstall node

stack overflowで似たような事象の質問を発見しましたが、ここでは、brew postinstall nodeが解決策として示されているようでした。
brew install npm "npm: command not found"

0
0
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
0
0