2012-05-06 69 views
6

我开始用node.j开发,遇到一个关于使用模块'强大'的问题。找不到模块强大 - Node.js

我有这样的错误:

Error: Cannot find module 'formidable'

下面是使用“安装NPM ls”的安装模块列表:

 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│  ├── [email protected] 
│  ├── [email protected] 
│  └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
└─┬ [email protected] 
    ├── [email protected] 
    ├── [email protected] 
    └─┬ [email protected] 
    ├─┬ [email protected] 
    │ └── [email protected] 
    ├── [email protected] 
    ├─┬ [email protected] 
    │ ├── [email protected] 
    │ └── [email protected] 
    └── [email protected] 

我补充一点,谁产生这种错误的唯一模块。

而且,我真的不明白封装一些模块的方式,似乎NPM在目录中直接将模块安装我使用的模块安装命令,我注意到,强大的已安装在快速/连接/模块在其第一次安装。

你可以给我更多关于模块安装树的信息。
感谢您的答复

干杯

+1

不要惊慌!保持冷静,想想你最近在哪里看到_formidable Node.js _... :) – gdoron

+0

我们可以请你看看你想要的代码强大吗?否则,我们怎么能做任何事情来帮助你... – Hubro

+0

我在我的file.js的开头直接调用模块的要求,我使用这个调用:var formidable = require(“formidable”) ; – bengo

回答

3

要理解模块分辨率,有一个看Modules documentation,尤其是Loading from node_modules Folders

For example, if the file at '/home/ry/projects/foo.js' called require('bar.js') , then node would look in the following locations, in this order:

  • /home/ry/projects/node_modules/bar.js
  • /home/ry/node_modules/bar.js
  • /home/node_modules/bar.js
  • /node_modules/bar.js

NPM利用了这种通过将模块安装到:

./node_modules/{module} 

所以,当你使用npm install formidable,它会创建并安装该模块为:

./node_modules/formidable 

但是,这意味着只有当前目录中的脚本(包括子目录)才能成功使用require('formidable')

./foo.js 
./lib/bar.js 
./src/baz.js 
./src/sub/qux.js 

但是,您可以安装模块,为“全球”,但你必须明确地要求它与-g--global

npm install -g formidable 

然后,系统上的任何脚本应该能够require('formidable')


至于树输出,你目前有5个可从当前目录中安装的模块:

  • express
  • formidable
  • node-inspector
  • npm
  • socket.io

树中的所有其他内容都是这些模块的依赖关系及其依赖关系等的列表,但只有这5个可用于脚本中的require(...)

+0

谢谢您的信息,非常有用 – bengo

5

接受的答案看起来非常全面,正确的,但这种工作对我来说:

npm install -d 

d代表依赖(我认为)

+0

+1它帮助我_C:\ Users \ yash \ AppData \ Roaming \ npm-cache \ selenium-webdriver \ 2.47.0 \ package \ example_'ctrl + shift +打开命令窗口'然后使用'npm install -d'安装所有的依赖关系,然后运行一个'node google_search.js'的例子 – Yash