Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not find async after installation

Tags:

node.js

npm

Today, I get strange thing that after i install async in global, nodejs reports it can not find the module.following is the workflow

  1. install async

    npm install -g async

  2. make sure async exists

    npm list -g async

get this output:

 /usr/local/lib ├── [email protected] └─┬ [email protected]   └─┬ [email protected]     └─┬ [email protected]       └── [email protected]  

3.try to use it.

 I create a simple js file which only contains one statement: var async=require('async'); then execute the file via node, I get exception: 
 Error: Cannot find module 'async'     at Function.Module._resolveFilename (module.js:338:15)     at Function.Module._load (module.js:280:25)     at Module.require (module.js:364:17)     at require (module.js:380:17)     at Object. (/lxzhu/nodejs/asynctest/test.js:1:73)     at Module._compile (module.js:456:26)     at Object.Module._extensions..js (module.js:474:10)     at Module.load (module.js:356:32)     at Function.Module._load (module.js:312:12)     at Function.Module.runMain (module.js:497:10) 
like image 327
Liangxiong.ZHU Avatar asked Jan 19 '14 04:01

Liangxiong.ZHU


2 Answers

It is because you are installing async globally.

npm install async will put create a directory called node_modules, and the require lookup algorithm will find it there.

like image 92
bendecoste Avatar answered Oct 06 '22 00:10

bendecoste


A global installation of an NPM doesn't always mean that the module can be shared for multiple projects. This is a pretty popular misconception. You can read this blog post on nodejs.org for more information, but generally speaking, global modules are used for command line tools and other system utilities, not for modules to be used in your code.

So, ideally, you would need the modules locally for each of your projects.

like image 40
Munim Avatar answered Oct 06 '22 00:10

Munim