node.jsのバージョンを16.18.1→22.14.0に変更した

はじめに

node.jsのバージョンを確認すると、16.18.1だった。

Mac:react_todo shibatahiroshitaka$ node -v
v16.18.1

これを最新にアップデートしていこう。

nvmを使う

Node Version Managerを使用すると、複数のNode.jsのバージョンを簡単に管理できる。

「curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash」で、nvmをインストールする。

Mac:react_todo shibatahiroshitaka$ curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
=> Downloading nvm from git to '/Users/shibatahiroshitaka/.nvm'
=> Cloning into '/Users/shibatahiroshitaka/.nvm'...
remote: Enumerating objects: 382, done.
remote: Counting objects: 100% (382/382), done.
remote: Compressing objects: 100% (325/325), done.
remote: Total 382 (delta 43), reused 181 (delta 29), pack-reused 0 (from 0)
Receiving objects: 100% (382/382), 386.80 KiB | 3.22 MiB/s, done.
Resolving deltas: 100% (43/43), done.
* (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /Users/shibatahiroshitaka/.bash_profile
=> Appending bash_completion source string to /Users/shibatahiroshitaka/.bash_profile
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:

/Users/shibatahiroshitaka/.npm-global/lib
├── create-nuxt-app@5.0.0
└── n@9.0.1
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:

     $ nvm use system
     $ npm uninstall -g a_module

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

nvmのインストール後に適応しようとしたら、エラーが出た。まず、.bashrcがないと言われた。次に、.zsshrcもないと言われた。

Mac:react_todo shibatahiroshitaka$ source ~/.bashrc
bash: /Users/shibatahiroshitaka/.bashrc: No such file or directory
Mac:react_todo shibatahiroshitaka$ source ~/.zsshrc
bash: /Users/shibatahiroshitaka/.zsshrc: No such file or directory

デフォルトのシェルを確認

echo $SHELLでデフォルトのシェルを確認すると、bashであることが確認できた。

Mac:react_todo shibatahiroshitaka$ echo $SHELL
/bin/bash

bash_profileに書き込み

sourceで書き込みを行おうとしたら、command not foundが返されたので、touchでbash_profileを作成した。その後にもう一度sourceを行ったが、状況は変わっていなかった。

Mac:react_todo shibatahiroshitaka$ source ~/.bash_profile
bash: pyenv: command not found
Mac:react_todo shibatahiroshitaka$ touch ~/.bash_profile
Mac:react_todo shibatahiroshitaka$ source ~/.bash_profile
bash: pyenv: command not found

nvm ls-remoteで最新のNode.jsバージョンを確認

Mac:react_todo shibatahiroshitaka$ nvm ls-remote
        v0.1.14
        v0.1.15
        v0.1.16
// 省略
       v22.14.0   (Latest LTS: Jod)
        v23.0.0
        v23.1.0
        v23.2.0
        v23.3.0
        v23.4.0
        v23.5.0
        v23.6.0
        v23.6.1
        v23.7.0
        v23.8.0
        v23.9.0
       v23.10.0

22.14.0にLatestと書かれていたので、こちらをインストールしていく。

nvm install 22.14.0

22.14.0のNode.jsはインストールできたようだが、nvmと競合を起こしてしまう設定があるみたいだった。

Mac:react_todo shibatahiroshitaka$ nvm install 22.14.0
Downloading and installing node v22.14.0...
Downloading https://nodejs.org/dist/v22.14.0/node-v22.14.0-darwin-x64.tar.xz...
################################################################################################################################################ 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Your user’s .npmrc file (${HOME}/.npmrc)
has a `globalconfig` and/or a `prefix` setting, which are incompatible with nvm.
Run `nvm use --delete-prefix v22.14.0` to unset it.

nvm use –delete-prefix v22.14.0

先ほどのメッセージの通りに、

Run `nvm use --delete-prefix v22.14.0` to unset it.

コマンドを入力したところ、設定が上手くいったみたいだ。node -vでNode.jsのバージョンを確認すると、22.14.0であることが確認できた。

Mac:react_todo shibatahiroshitaka$ nvm use --delete-prefix v22.14.0
Now using node v22.14.0 (npm v10.9.2)
Mac:react_todo shibatahiroshitaka$ node -v
v22.14.0

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です