javascript 找不到强大的模块 - Node.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10467668/
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
Cannot find module formidable - Node.js
提问by bengo
I'm starting to develop with node.j,I meet an issue regarding the using of the module 'formidable'.
我开始使用 node.j 进行开发,我遇到了一个关于使用模块“强大”的问题。
I have this error:
我有这个错误:
Error: Cannot find module 'formidable'
错误:找不到模块“强大”
Here is the module list installed using 'npm ls installed' :
这是使用 'npm ls installed' 安装的模块列表:
├─┬ [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]
I add that it is the only module who generate this error.
我补充说,它是唯一产生此错误的模块。
Also, I don't really understand the way are encapsulated some module, it appears that npm is installing the module directly in the directory I'm using the module installation command, and I notice that formidable has been installed in the express/connect/ module on its first installation.
另外,我不太明白封装一些模块的方式,看来npm是直接在我使用模块安装命令的目录中安装模块的,我注意到express/connect/中已经安装了formidable模块在其第一次安装。
Can you give me more information about the module installation tree.
Thank for your replies
你能给我更多关于模块安装树的信息吗?
感谢您的回复
Cheers
干杯
采纳答案by Jonathan Lonowski
To understand module resolution, have a look at the Modules documentation, especially Loading from node_modules
Folders.
要了解模块解析,请查看模块文档,尤其是从node_modules
文件夹加载。
For example, if the file at
'/home/ry/projects/foo.js'
calledrequire('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
例如,如果文件
'/home/ry/projects/foo.js'
名为require('bar.js')
,则节点将按以下顺序查找以下位置:
/home/ry/projects/node_modules/bar.js
/home/ry/node_modules/bar.js
/home/node_modules/bar.js
/node_modules/bar.js
NPM takes advantage of this by installing modules into:
NPM 通过将模块安装到以下位置来利用这一点:
./node_modules/{module}
So, when you use npm install formidable
, it will create and install the module into:
因此,当您使用 时npm install formidable
,它将创建模块并将其安装到:
./node_modules/formidable
But, this means that only scripts within the current directory, including sub-directories, will succeed in using require('formidable')
:
但是,这意味着只有当前目录中的脚本(包括子目录)才能成功使用require('formidable')
:
./foo.js
./lib/bar.js
./src/baz.js
./src/sub/qux.js
You can however install modules as "global," but you have to explicitly ask for it with -g
or --global
:
但是,您可以将模块安装为“全局”,但您必须使用-g
或明确要求它--global
:
npm install -g formidable
Then, any script on the system should be able to require('formidable')
.
然后,系统上的任何脚本都应该能够require('formidable')
.
As for the tree output, you current have 5 installed modules available from the current directory:
至于树输出,您当前有 5 个可从当前目录使用的已安装模块:
express
formidable
node-inspector
npm
socket.io
express
formidable
node-inspector
npm
socket.io
Everything else in the tree is a list of these modules' dependencies, and their dependencies, etc., but only these 5 are available for require(...)
within your scripts.
树中的其他所有内容都是这些模块的依赖项及其依赖项等的列表,但require(...)
在您的脚本中只有这 5 个可用。
回答by captainclam
The accepted answer looks very comprehensive and correct, but this worked for me:
接受的答案看起来非常全面和正确,但这对我有用:
npm install -d
d stands for dependencies (I think)
d 代表依赖(我认为)