LoginSignup
224
213

More than 3 years have passed since last update.

npmのグローバルインストールとローカルインストール

Last updated at Posted at 2014-05-28

グローバルインストール

こんな感じ

npm install -g bower

こちらの記事

npmのインストール場所にパッケージをインストールします。
付けない場合は,カレントディレクトリのnode_modules内にインストールされます。

グローバルインストールはそうなんですけれど、私の場合はnodebrewでnodeとnpmを入れているので、

npm list -g
/Users/teru1/.nodebrew/current/lib

ここ配下にnode_modulesがあり、ここにインストールされている様子です。これはよくわかりました。

ローカルインストール

こちらの記事にはローカルインストールはカレントディレクトリのnode_modules内にインストールと書いてあるので、
ホームディレクトリ配下にディレクトリ作って、そこでnpm installやったんですけど、
そのディレクトリの親であるホームディレクトリのnode_modulesにインストールされました。どうしてそんなディレクトリがあるのかといいますと、前にここでnpm installとかやった時に作られたものなのでしょう。

親ディレクトリにnode_modulesディレクトリがある場合はそこにインストールされる?

試す

親にnode_modulesディレクトリが無いディレクトリにて

$ pwd
/Users/Shared

↑この階層内にはnode_modulesディレクトリはありません

↓ディレクトリ作って、移動して、install

$ mkdir sample_npm
$ cd sample_npm/
$ npm install bower

↓node_modulesが作られて、そこにインストールされた!

$ ls
total 0
drwxr-xr-x  3 teru1  wheel  102  5 27 17:01 ./
drwxrwxrwt  8 root   wheel  272  5 27 17:01 ../
drwxr-xr-x  4 teru1  staff  136  5 27 17:02 node_modules/

親にnode_modulesディレクトリを作って

さっきの始まりと同じ状態にして、/Users/Sharedにnode_modulesディレクトリを作成します。

$ pwd
/Users/Shared
$ mkdir node_modules
$ ls
total 16
drwxrwxrwt  8 root   wheel   272  5 28 11:45 ./
drwxr-xr-x  5 root   admin   170 11  9  2013 ../
-rw-r--r--@ 1 teru1  wheel  6148 12 14 01:21 .DS_Store
-rw-r--r--  1 root   wheel     0  8 25  2013 .localized
drwxrwxrwx@ 5 teru1  wheel   170 11 10  2013 SC Info/
drwxr-xr-x  6 root   wheel   204 11 17  2013 SplashtopStreamer/
drwxrwxrwx@ 2 teru1  wheel    68 11  9  2013 adi/
drwxr-xr-x  2 teru1  wheel    68  5 28 11:45 node_modules/

↓ディレクトリ作って、移動して、install

$ mkdir sample_npm
$ cd sample_npm/
$ npm install bower

↓ここにディレクトリは出来ない

$ ls
total 0
drwxr-xr-x  2 teru1  wheel   68  5 28 11:47 ./
drwxrwxrwt  9 root   wheel  306  5 28 11:47 ../

mkdirで作った上の階層のnode_modulesにインストールされたようです。なるほど。

$ ls ../node_modules/
total 0
drwxr-xr-x   4 teru1  wheel  136  5 28 11:48 ./
drwxrwxrwt   9 root   wheel  306  5 28 11:47 ../
drwxr-xr-x   3 teru1  wheel  102  5 28 11:48 .bin/
drwxr-xr-x  18 teru1  staff  612  5 28 11:48 bower/
224
213
2

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
224
213