node.js 安装后找不到异步
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21213155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can not find async after installation
提问by Liangxiong.ZHU
Today, I get strange thing that after i install async in global, nodejs reports it can not find the module.following is the workflow
今天,我发现奇怪的是,在全局安装 async 后,nodejs 报告它找不到模块。以下是工作流程
install async
npm install -g async
make sure async exists
npm list -g async
安装异步
npm install -g 异步
确保异步存在
npm list -g 异步
get this output:
得到这个输出:
/usr/local/lib ├── [email protected] └─┬ [email protected] └─┬ [email protected] └─┬ [email protected] └── [email protected]
3.try to use it.
3.尝试使用它。
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)
回答by bendecoste
It is because you are installing async globally.
这是因为您正在全局安装 async。
npm install asyncwill put create a directory called node_modules, and the requirelookup algorithm will find it there.
npm install async将 put 创建一个名为 的目录node_modules,require查找算法会在那里找到它。
回答by Munim
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.orgfor 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.
NPM 的全局安装并不总是意味着该模块可以为多个项目共享。这是一个非常流行的误解。您可以在 nodejs.org 上阅读这篇博文以获取更多信息,但一般来说,全局模块用于命令行工具和其他系统实用程序,而不是用于代码中的模块。
So, ideally, you would need the modules locally for each of your projects.
因此,理想情况下,您的每个项目都需要本地模块。
回答by Damini Suthar
async installed globally. For that we have to create and install modules of async.
async 全局安装。为此,我们必须创建和安装异步模块。
npm install async --save
npm install async --save
this command line add files in node_modules folder.
此命令行在 node_modules 文件夹中添加文件。
回答by Navin Kumar
if dont Find Any Module like
Cannot find module 'sql' , Cannot find module 'nodemailer'
then use npm install and module name which is can not found.
npm install async
如果没有找到任何模块,如
找不到模块 'sql' ,找不到模块 'nodemailer' 则使用 npm install 和找不到的模块名称。npm 安装异步
回答by Fred Stark
One way of using globally installed modules in multiple projects is to use the npm linkcommand
在多个项目中使用全局安装的模块的一种方法是使用npm link命令
npm linkwill create a symlink of the globally installed package into your apps node_modulesdirectory
npm link将在您的应用程序node_modules目录中创建全局安装包的符号链接
回答by Liangxiong.ZHU
Finally, i get answer from http://nodejs.org/api/modules.html.
最后,我从 http://nodejs.org/api/modules.html 得到答案。
After install globally, i need to put its subdirectory to NODE_PATH to make it appears in node's search path.
全局安装后,我需要将其子目录放到 NODE_PATH 中,以使其出现在节点的搜索路径中。
Also, as the document said, It is suggested to store module locally and NODE_PATH is for version compatibility and we should not use it any more.
另外,正如文档所说,建议将模块存储在本地,NODE_PATH 是为了版本兼容,我们不应再使用它。

