8
12

More than 3 years have passed since last update.

Node.js nを使い複数バージョンを共存させる

Last updated at Posted at 2020-09-06

仕事でnode.jpを使用する機会があったのですが、
複数のシステムで様々なnodeのバージョンを使う機会があり、仮想環境(CentOs)でnodeのバージョン切り替えれるようにしました

Node.js と npm インストール

とりあえず、nodeとnpmをインストールします (CentOs)

yum install gcc gcc-c++
yum install nodejs npm –enablerepo=epel

※ついでにMacの場合は以下のコマンドで

brew install node

n のインストール

npm install -g n

nのパスを通す

~/.bash_profileを使用しパスを指定する
※ない場合は、作成してください。~/.profile, ~/.bashrcでも自分の好みのもので構いません

cd
vi .bash_profile

.bash_profileに以下の文言を買い込みます

export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH

調整を適用

source .bash_profile

これで準備できました!

特定バージョンのインストール

最新バージョン

$ n –stable
$ n –latest
$ n latest

※これでうまくいかないことがあるみたいです、、こちらを参考に[n lts]を使用するとうまくいきました
https://blog1.mammb.com/entry/2019/11/26/090000

n lts
n latest

特定バージョン

$ n 5.7.1

バージョン切り替え

nコマンドを使用すると矢印キーとEnterキーで切り替えることができます

# n

ο   node/10.17.0
    node/12.18.3

Use up/down arrow keys to select a version, return key to install, d to delete, q to quit

これでnodeのバージョンを気楽に切り替えれるようになりました、本当に助かる

参考文献

https://qiita.com/jaxx2104/items/2277cec77850f2d83c7a
https://blog1.mammb.com/entry/2019/11/26/090000
https://qiita.com/bigplants/items/2d75bf552e0da4a7e822

8
12
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
8
12