简体   繁体   English

Node.js - 找不到模块

[英]Node.js - cannot find module

I'm using Node Boilerplate and 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/535 which 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? 源代码在两个文件夹中完全相同,它在一个副本中正常工作而在另一个副本中失败?

Do the two folders share a common parent? 这两个文件夹共享一个共同的父级吗? Node looks for modules in ~/.node_modules and /node_modules in your app dir. Node在app dir中的〜/ .node_modules和/ node_modules中查找模块。

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). 对于错误信息感到抱歉 - 这两个文件夹并不完全相同(我需要更好地学习unix diff)。 And there it require("connect") in code 它需要(“连接”)代码

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加载。

@see node.js express module not loading properly in the last comment, maybe it helps @see node.js表示模块在最后一条评论中没有正确加载 ,也许有帮助

the basic idea behind it is to install the module inside your application folder, not on a system level 它背后的基本思想是将模块安装在应用程序文件夹中,而不是在系统级别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM