62
51

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

npm install -gでグローバルインストールしたのに見つけてくれない

Last updated at Posted at 2016-05-06

問題

npm install -g <module>
であるモジュールをインストールしたのに実行時見つからないとエラーが出た。

実行結果

$ npm install -g request
request@2.72.0 /home/vagrant/.nvm/versions/node/v0.12.4/lib/node_modules/request
├── is-typedarray@1.0.0
├── aws-sign2@0.6.0
├── oauth-sign@0.8.2
├── forever-agent@0.6.1
├── tunnel-agent@0.4.3
├── caseless@0.11.0
├── stringstream@0.0.5
├── isstream@0.1.2
├── json-stringify-safe@5.0.1
├── extend@3.0.0
├── node-uuid@1.4.7
├── combined-stream@1.0.5 (delayed-stream@1.0.0)
├── qs@6.1.0
├── tough-cookie@2.2.2
├── mime-types@2.1.11 (mime-db@1.23.0)
├── aws4@1.3.2 (lru-cache@4.0.1)
├── form-data@1.0.0-rc4 (async@1.5.2)
├── bl@1.1.2 (readable-stream@2.0.6)
├── http-signature@1.1.1 (assert-plus@0.2.0, jsprim@1.2.2, sshpk@1.8.3)
├── hawk@3.1.3 (cryptiles@2.0.5, sntp@1.0.9, boom@2.10.1, hoek@2.16.3)
└── har-validator@2.0.6 (pinkie-promise@2.0.1, commander@2.9.0, chalk@1.1.3, is-my-json-valid@2.13.1)

$ node getall.js 
module.js:338
    throw err;
    ^
Error: Cannot find module 'request'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/vagrant_data/p062/getall.js:2:15)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)

調査

Node.js実行時にモジュールを探すパス①にグローバルインストールしたモジュールが格納されるディレクトリ②が追加されていない場合にこうなるらしいことがわかった。

①の確認方法

$ node -e "console.log(global.module.paths)"
[ '/vagrant_data/p062/node_modules',
  '/vagrant_data/node_modules',
  '/node_modules' ]

②の確認方法

$ npm root -g
/home/vagrant/.nvm/versions/node/v0.12.4/lib/node_modules

解決方法

ホームディレクトリにある~/.bash_profileにグローバルなパスを追加する

echo export NODE_PATH=$(npm root -g) >> ~/.bash_profile
source ~/.bash_profile
62
51
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
62
51

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?