以下の n を使った Node.js のバージョン管理関連の話です。
●tj/n: Node version management
https://github.com/tj/n
以下の M4 Mac mini をセットアップした時に久しぶりに使ったのですが、その時のメモです。
セットアップ
公式の記載で複数のセットアップ方法が書かれていますが、自分がセットアップした際は Homebrew を使いました。
インストールは下記のコマンドで行いました。
brew install n
設定
インストールに関わる設定の話です。
自分の Mac の環境でセットアップしている中、以下の「/usr/local への書き込み権限関連の話」の対応を行いました。
それぞれについて、軽くメモしておきます。
対応方法1
対応方法 1つ目は「change the ownership of the relevant directories to yourself (see below)」と書かれたものです。
以下は公式手順の転載ですが、「sudoコマンドで /usr/local/n を作成してユーザーへの権限の付与を行う」といったことや、「/usr/local/ 以下の bin・lib・include・share への権限設定(フォルダがない場合は作成から)」ということをやっている感じです。
# make cache folder (if missing) and take ownership
sudo mkdir -p /usr/local/n
sudo chown -R $(whoami) /usr/local/n
# make sure the required folders exist (safe to execute even if they already exist)
sudo mkdir -p /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
# take ownership of Node.js install destination folders
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
対応方法2
対応方法 2つ目は「tell n to use a custom location where you do have write permissions (see N_PREFIX)」という内容です。
「see N_PREFIX」と書かれている内容に従いページ内を見ていくと、以下の記載があります。
「$HOME/.n」(「~/.n」)という、ユーザーのホームディレクトリ直下に「.n」というフォルダが作られるようにし、その下を n で使うというものです。
例えば zsh を使っている場合の対応の例としては、以下の設定を .zshrc
に追記します。
export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH
変更後、上記の変更を即時反映させる場合は以下を実行します。
source ~/.zshrc
対応方法3
対応方法 3つ目は「put sudo in front of the command to run it as super user」と書かれた、「sudoコマンドを使った実行」という対応方法です。
上記のいずれかの対応を行うことで、n のセットアップ完了です。
自分の設定
上記の方法について、自分は当初は 2つ目でやってみていたのですが、以下の記事に書いた事象が起こり、上記の 1つ目のやり方に変更しました。
●Claude for Desktop で MCP を使おうとした際のエラーに関する対処メモ(npxコマンドまわり) - Qiita
https://qiita.com/youtoy/items/21ebf4df0bfe7d8201e4