Today, I get strange thing that after i install async in global, nodejs reports it can not find the module.following is the workflow
install async
npm install -g async
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With