Node.js - 找不到模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5291027/
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
Node.js - cannot find module
提问by user80805
I'm using Node Boilerplateand it all worked fine until I decided create another project on top of it(in another dir).
我正在使用Node Boilerplate并且一切正常,直到我决定在它之上(在另一个目录中)创建另一个项目。
Now I have exactly the same code base(this project AS IS) in two different folders. I can run one of it without any problems but another one is failing with:
现在我在两个不同的文件夹中有完全相同的代码库(这个项目原样)。我可以毫无问题地运行其中一个,但另一个失败:
% node app.js
node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'connect/middleware/router'
at Function._resolveFilename (module.js:299:11)
at Function._load (module.js:245:25)
at require (module.js:327:19)
at Object.<anonymous> (/home/gotts/Sites/nodejs-uploadr/lib/express/lib/express/server.js:17:14)
at Module._compile (module.js:383:26)
at Object..js (module.js:389:10)
at Module.load (module.js:315:31)
at Function._load (module.js:276:12)
at require (module.js:327:19)
at Object.<anonymous> (/home/gotts/Sites/nodejs-uploadr/lib/express/lib/express/index.js:28:31)
There is similar problem explained here - https://github.com/visionmedia/express/issues/535which says that the problem is in the incompatible version of connect/express.
这里解释了类似的问题 - https://github.com/visionmedia/express/issues/535表示问题出在不兼容的 connect/express 版本中。
But how is this possible? Source code is exactly the same in two folders and it work fine in one copy and fails in another?
但这怎么可能呢?两个文件夹中的源代码完全相同,它在一个副本中工作正常而在另一个副本中失败?
采纳答案by user80805
OK, let me answer my own question:
好的,让我回答我自己的问题:
Sorry for misinformation - those two folders were not exactly the same(I'm gonna need to learn unix diff better). And there it require("connect") in code
对不起,错误信息 - 这两个文件夹不完全相同(我需要更好地学习 unix diff)。它在代码中需要(“连接”)
Local checkout of connect into /lib folder takes precedence but if it's not there - connect will be loaded from the ~/.node_modules as Ricardo previously set.
连接到 /lib 文件夹的本地检出优先,但如果它不在那里 - 连接将从里卡多之前设置的 ~/.node_modules 加载。
回答by Ricardo Tomasi
Do the two folders share a common parent? Node looks for modules in ~/.node_modules and /node_modules in your app dir.
这两个文件夹是否共享一个共同的父文件夹?Node 在您的应用程序目录中的 ~/.node_modules 和 /node_modules 中查找模块。
回答by Georgiana
@see node.js express module not loading properlyin the last comment, maybe it helps
@see node.js express 模块在最后一条评论中没有正确加载,也许有帮助
the basic idea behind it is to install the module inside your application folder, not on a system level
它背后的基本思想是将模块安装在您的应用程序文件夹中,而不是在系统级别

