nvm&npmの環境で入れたモジュールってバージョン毎にどこあるのか調べた。

f:id:yassan0627:20171229071433p:plain 前に、yoを入れたのだけど、

yassan.hatenablog.jp

Node.jsをnvmでVer.切り替え出来るようにしてるので、インストールしたモジュールってどこに行くのか?
って思ったので簡単に調べた。

結論: -g指定してグローバルインストールしているけど、実行バージョンのみにインストールしている。

改めて、nvm読む。

環境は、NPM_CONFIG_PREFIXを見ているみたい。

マニュアルは、、、man npmってやっても存在しません。。 npm --help としたら、npm help npmで分かるそうな。
ver.依存あるし当然っちゃ当然。

$ npm help npm
 :

DIRECTORIES
       See npm help 5 npm-folders to learn about where npm puts stuff.

       In particular, npm has two modes of operation:

       · global mode:
         npm installs packages into the install prefix at prefix/lib/node_modules and bins are installed in prefix/bin.

       · local mode:
         npm installs packages into the current project directory, which defaults to the  current  working  directory.   Packages  are
         installed to ./node_modules, and bins are installed to ./node_modules/.bin.

       Local mode is the default.  Use -g or --global on any command to operate in global mode instead.

グローバルモード(インストール時に -g 指定)した場合は、prefix at prefix/lib/node_modules と prefix/bin にインストール。 デフォルトとはローカルモードで、カレントに./node_modulesを使ってそこに入れる。と。なるほど。

prefixってどこかなぁ、、、

$ npm config edit
 :
; prefix=/home/yassan/.nvm/versions/node/v8.9.3
   :

コメントアウトされてるけど、デフォルトは/home/yassan/.nvm/versions/node/v8.9.3ですね。

試しに別の環境で、2つのバージョンをインストール後に、ローカルモードでインストール(npm install yo)したら、カレントにnode_modulesディレクトリを作って、そこにインストールしてた。 しかも、パスが通ってないので、未設定だとyo使えない(当たり前)

んで、グローバルモードでyoをインストール。

$ npm install -g yo
/home/yassan/.nvm/versions/node/v8.9.3/bin/yo -> /home/yassan/.nvm/versions/node/v8.9.3/lib/node_modules/yo/lib/cli.js
/home/yassan/.nvm/versions/node/v8.9.3/bin/yo-complete -> /home/yassan/.nvm/versions/node/v8.9.3/lib/node_modules/yo/lib/completion/index.js

> spawn-sync@1.0.15 postinstall /home/yassan/.nvm/versions/node/v8.9.3/lib/node_modules/yo/node_modules/spawn-sync
> node postinstall


> yo@2.0.0 postinstall /home/yassan/.nvm/versions/node/v8.9.3/lib/node_modules/yo
> yodoctor

となってたので、実行しているバージョン先と関連付けてますね。 実際に存在確認。

yassan:~/.nvm/versions ((v0.33.8)) $ ll node/v8.9.3/bin/
合計 34388
drwxrwxr-x 2 yassan yassan     4096 12月 29 06:47 ./
drwxrwxr-x 7 yassan yassan     4096 12月 29 05:38 ../
-rwxrwxr-x 1 yassan yassan 35200878 12月  8 23:09 node*
lrwxrwxrwx 1 yassan yassan       38 12月  8 23:10 npm -> ../lib/node_modules/npm/bin/npm-cli.js*
lrwxrwxrwx 1 yassan yassan       38 12月  8 23:10 npx -> ../lib/node_modules/npm/bin/npx-cli.js*
lrwxrwxrwx 1 yassan yassan       33 12月 29 06:47 yo -> ../lib/node_modules/yo/lib/cli.js*
lrwxrwxrwx 1 yassan yassan       46 12月 29 06:47 yo-complete -> ../lib/node_modules/yo/lib/completion/index.js*
yassan:~/.nvm/versions ((v0.33.8)) $ ll node/v9.3.0/bin/
合計 34212
drwxrwxr-x 2 yassan yassan     4096 12月 12 18:38 ./
drwxrwxr-x 7 yassan yassan     4096 12月 29 06:21 ../
-rwxrwxr-x 1 yassan yassan 35021636 12月 12 18:37 node*
lrwxrwxrwx 1 yassan yassan       38 12月 12 18:38 npm -> ../lib/node_modules/npm/bin/npm-cli.js*
lrwxrwxrwx 1 yassan yassan       38 12月 12 18:38 npx -> ../lib/node_modules/npm/bin/npx-cli.js*

確かに、実行バージョン(v8.9.3)にはyoはあるけど、使っていないバージョンには入ってませんね。

納得したのでこの辺で。