わすれっぽいきみえ

みらいのじぶんにやさしくしてやる

【2020年版】Node.jsのバージョンをnodebrewで切り替えられるようにした

kimikimi714.com

ここにある内容を2020年になった今再度することになったので備忘録として残す。

作業環境はWSL2上のUbuntu 20.04LTS。

nodebrewのインストール

$ brew install nodebrew
==> Downloading https://github.com/hokaccha/nodebrew/archive/v1.0.1.tar.gz
==> Downloading from https://codeload.github.com/hokaccha/nodebrew/tar.gz/v1.0.1                                        ######################################################################## 100.0%                                         
==> Caveats
You need to manually run setup_dirs to create directories required by nodebrew:
    /home/linuxbrew/.linuxbrew/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=/home/linuxbrew/.linuxbrew/var/nodebrew                                                                                                                                                                                  Bash completion has been installed to:
    /home/linuxbrew/.linuxbrew/etc/bash_completion.d
==> Summary 
🍺  /home/linuxbrew/.linuxbrew/Cellar/nodebrew/1.0.1: 8 files, 69.6KB, built in 0 seconds

nodebrewをインストールした後の下準備

$ /home/linuxbrew/.linuxbrew/opt/nodebrew/bin/nodebrew setup_dirs
$ echo "export PATH=$HOME/.nodebrew/current/bin:$PATH" >> .bashrc

nodebrewの使い方確認

$ nodebrew -v
nodebrew 1.0.1                                                                                                                                                                                                                                  Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install <version> (from binary)
    nodebrew compile <version>            Download and install <version> (from source)
    nodebrew install-binary <version>     Alias of `install` (For backword compatibility)
    nodebrew uninstall <version>          Uninstall <version>
    nodebrew use <version>                Use <version>
    nodebrew list                         List installed versions
    nodebrew ls                           Alias for `list`
    nodebrew ls-remote                    List remote versions
    nodebrew ls-all                       List remote and installed versions
    nodebrew alias <key> <value>          Set alias
    nodebrew unalias <key>                Remove alias
    nodebrew clean <version> | all        Remove source file
    nodebrew selfupdate                   Update nodebrew
    nodebrew migrate-package <version>    Install global NPM packages contained in <version> to current version
    nodebrew exec <version> -- <command>  Execute <command> using specified <version>                                                                                                                                             Example:
    # install
    nodebrew install v8.9.4                                                                                                                                                                                                                         
    # use a specific version number
    nodebrew use v8.9.4

nodebrewで今インストールできるNode.jsの確認

ioってまだあったんだ…。

$ nodebrew ls-remote
v0.0.1    v0.0.2    v0.0.3    v0.0.4    v0.0.5    v0.0.6

(略)

v12.0.0   v12.1.0   v12.2.0   v12.3.0   v12.3.1   v12.4.0   v12.5.0   v12.6.0
v12.7.0   v12.8.0   v12.8.1   v12.9.0   v12.9.1   v12.10.0  v12.11.0  v12.11.1
v12.12.0  v12.13.0  v12.13.1  v12.14.0  v12.14.1  v12.15.0  v12.16.0  v12.16.1
v12.16.2  v12.16.3  v12.17.0

(略)

v14.0.0   v14.1.0   v14.2.0   v14.3.0

io@v1.0.0 io@v1.0.1 io@v1.0.2 io@v1.0.3 io@v1.0.4 io@v1.1.0 io@v1.2.0 io@v1.3.0
(略)
io@v3.0.0 io@v3.1.0 io@v3.2.0 io@v3.3.0 io@v3.3.1

nodebrewを使ってNode.jsをインストール

nodejs.org

現時点でのLTSはv12.17.0なので、そのバージョンをインストールする。

$ nodebrew install v12.17.0
Fetching: https://nodejs.org/dist/v12.17.0/node-v12.17.0-linux-x64.tar.gz
################################################################## 100.0%
Installed successfully

$ nodebrew use v12.17.0
use v12.17.0

$ node -v
v12.17.0

入ったー。