LoginSignup
0
1

More than 5 years have passed since last update.

nvmで同じバージョンのnodeを分けて管理する

Posted at

背景

nvmで異なるバージョンのnodeを管理するのは大変便利だけど、pythonのvenvみたいに、「同じバージョンだけど違うグローバルライブラリ」のセットを管理したい場合があるので、その方法を調査してみた。

調査

どうやら、ずっと昔から要望としてはあるものの、「nvmとしては実装する予定がない」らしい。

Since this is a less used feature and it's not that hard to do by hand, I'd rather not bloat the project with it built-in.

Do you mean, installing multiple copies of the same version of node with separate global namespaces and npms?

If that's what you want, why wouldn't you just have multiple copies of nvm in different $NVM_DIRs?

The simple solution to needing multiple installs of node that have the same version number is, to use different NVM_DIR folders (and different installations of nvm), each with their own copy of the node version you want.

結論

nvmとして相当する機能はない、そしてこれからも実装される可能性はなかったが、NVM_DIR環境変数でnvm本体を複数インストールできるので、この方法で同じバージョンのnodeを隔離することは可能。

nvmを別のフォルダーにインストール

READMEに記載された通りに、.nvm-myenvにnvmをインストールする:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh \
| NVM_DIR=~/.nvm-myenv bash

nvmの切り替え

本来.profile.bashrcに追加されるnvmのscriptを切替時に手動で叩く:

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

注意点

  • nvmコマンドはPATHで参照される実行ファイルではなく、nvm.shが実行されるたびにshellに設定されるので、上記の方法で切り替えた後にもし新しいshell sessionに入ったら、もう一度上記のコマンドを叩かないと、nvmコマンドは見つからなくなる。
    • 普段、nvm使うときは.profile/.bashrcに上記のコマンドを入れてあるはずだけど、複数のnvmがあるときに「新しいshell sessionは常に.profile/.bashrcに設定されている方を使っしまう」という問題があるので、.profile/.bashrcからは削除したほうが混乱を防げるだろう。
0
1
1

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
1