nodenvをanyenv経由に

Blog

前回の記事では nodenv を使用して「開発環境を楽に」しました。
今回はnodenvをanyenv経由に変更します。

anyenvとは?

anyenv – all in one for **env
This is a simple wrapper for rbenv style environment managers. You don’t have to git clone or modify your shell profile for each **env anymore if you install anyenv.

anyenvはenvと名のつくバージョン管理ツールを一括で管理するもの・・・です。
2018年1月現在、anyenvでは以下の「**env」を管理できます。

anyenvのメリットはまさしくこちら。

You don’t have to git clone or modify your shell profile for each **env anymore if you install anyenv.

ということで、nodenvもanyenv経由に変更します。

anyenvインストール

次の手順でanyenv,nodenvをインストールします。

  1. 既存のnodenvをアンインストール
  2. anyenvをインストール
  3. anyenv経由でnodenvをインストール

(1)既存のnodenvをアンインストール

アンインストールの方法はnodenvの公式サイトに記載があります。
今回は簡易的に削除します。

Uninstalling nodenv
1.To disable nodenv managing your Node versions, simply remove the nodenv init line from your shell startup configuration. This will remove nodenv shims directory from $PATH, and future invocations like node will execute the system Node version, as before nodenv.
nodenv will still be accessible on the command line, but your Node apps won’t be affected by version switching.

$ vi .bash_profile
$ source .bash_profile

.bash_profile から、nodenvへのPATHをコメントアウト。

#export PATH="$HOME/.nodenv/bin:$PATH"
#eval "$(nodenv init -)"

対象はこちら。

$ cat .node-version
9.2.0
$ node -v
v6.4.0

アンインストールされたことを確認。

(2)anyenvをインストール

anyenv公式サイトに従いインストールします。

$ git clone https://github.com/riywo/anyenv ~/.anyenv
$ echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(anyenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l

弊社は全員macなので・・・「.your_profile」を「.bash_profile」に変えて実行します。

$ anyenv
anyenv
Usage: anyenv <command></command> []

Some useful anyenv commands are:
commands List all available anyenv commands
local Show the local application-specific Any version
global Show the global Any version
install Install a **env
uninstall Uninstall a specific **anv
version Show the current Any version and its origin
versions List all Any versions available to **env

See `anyenv help <command></command>' for information on a specific command.
<command></command> For full documentation, see: https://github.com/riywo/anyenv#readme
<command></command>

anyenvがインストールされたことを確認。

$ anyenv install --list
Available **envs:
Renv
crenv
denv
erlenv
exenv
goenv
hsenv
jenv
luaenv
ndenv
nenv
nodenv
phpenv
plenv
pyenv
rbenv
sbtenv
scalaenv
swiftenv

anyenvでインストールできる*env系を確認。

(3)anyenv経由でnodenvをインストール

$ anyenv install nodenv
$ exec $SHELL -l

さいごに、anyenv経由でnodenvをインストールします。

$ which nodenv
/Users/username/.anyenv/envs/nodenv/bin/nodenv

nodenvがインストールされたことを確認。

以降、前回の記事と同じです。

nodenv install --list
nodenv install 9.2.0
nodenv rehash
$ pwd
/Users/username/.anyenv/envs/nodenv/plugins

$ ls -l
total 0
drwxr-xr-x  14 username  group  476  1 19 21:51 node-build
drwxr-xr-x  11 username  group  374  1 19 21:51 nodenv-default-packages
drwxr-xr-x   9 username  group  306  1 19 21:51 nodenv-vars

なお、前回手動でインストールしたプラグイン node-build.git は、すでにインストールされていました。

$ cat .node-version
9.2.0
$ node -v
v9.2.0

最初に試したワークスペースに移動して確認。

nodenvとndenv

anyenvで経由で使用できる*env系には、Node.js管理ができるものが3つあります。

・nenv
・nodenv
・ndenv

うち、nodenvとndenvは
どちらもNode.jsのバージョン管理マネージャで、各開発環境毎にNode.jsを適用できます。
どちらも.node-versionを作ります。

プロジェクトによりnodenvとndenvを切り替える – to-R

前提として「nodenv」と「ndenv」では.node-versionに記載されるバージョンの表記が違います。
これにより複数のプロジェクトを横断している場合に、nodenvでバージョン指定されたものはnodenvでしか起動できず、ndenvで指定されたものはndenvでしか起動できないという現象になってしまいます。
(中略)
ndenvであればプロジェクトに合わせたnodeのバージョンが起動できるようになるのでvあり、vなし問題で悩まなくて済むようになります。

こちらの記事ではndenvを推していますが、
社内(製作者間)で統一さえされていれば生じない問題なので・・・。

現時点で開発が活発なnodenvを採用します。

参考