简体   繁体   English

NodeJS找不到自定义模块

[英]NodeJS cannot find custom module

I am trying to export a module to my routes file. 我正在尝试将模块导出到我的路由文件。

My file tree goes like 我的文件树像

routes.js
app.js
controllers/users.js
            posts.js

on my app.js I exported var route = require('./routes'); 在我的app.js我导出了var route = require('./routes'); and that works. 那行得通。 now on my routes.js I tried to export require('./controllers'); 现在在我的routes.js我试图导出require('./controllers'); .

But it keeps telling me that it cannot find module controllers . 但是它一直告诉我cannot find module controllers

Doing this works: 做到这一点:

require('./controllers/users')

But I am trying to follow expressjs sample initial projects format. 但是我试图遵循expressjs示例初始项目格式。 And it is bugging me because the example of expressjs shows: (express routes is a folder) 这让我expressjs因为expressjs的示例显示:(express route是一个文件夹)

var routes = require('./routes');

and loads it like 并像加载它

app.get('/', routes.index);

with no error. 没有错误。 and that ./routes is a folder. ./routes是一个文件夹。 I am just following the same principle. 我只是遵循相同的原则。

If you try to require a directory, it expects there to be an index.js file in that directory. 如果您尝试require一个目录,则它期望该目录中有一个index.js文件。 Otherwise, you have to simply require individual files: require('./controllers/users') . 否则,您仅需要简单的单个文件: require('./controllers/users') Alternatively, you can create an index.js file in the controllers directory and add the following: 或者,您可以在controllers目录中创建index.js文件,并添加以下内容:

module.exports.users = require('./users');
module.exports.posts = require('./posts');

and then import: var c = require('./controllers'); 然后导入: var c = require('./controllers'); . You can then use them via c.users and c.posts . 然后,您可以通过c.usersc.posts使用它们。

You have to understand how require() works. 您必须了解require()的工作方式。

In your case it fails to find a file named controller.js so it assumes it's a directory and then searches for index.js specifically. 在您的情况下,它找不到名为controller.js的文件,因此假定它是目录,然后专门搜索index.js That is why it works in the express example. 这就是为什么它在示例中起作用。

For your usecase you can do something like this - 对于您的用例,您可以执行以下操作-

var controllerPath = __dirname + '/controllers';
fs.readdirSync(controllerPath).forEach(function(file) {
    require(controllerPath + '/' + file);
});

From: http://nodejs.org/api/modules.html 来自: http : //nodejs.org/api/modules.html

LOAD_AS_DIRECTORY(X) LOAD_AS_DIRECTORY(X)

  1. If X/package.json is a file, a. 如果X / package.json是文件,则 Parse X/package.json, and look for "main" field. 解析X / package.json,然后查找“ main”字段。 b. b。 let M = X + (json main field) c. 令M = X +(json主字段)c。 LOAD_AS_FILE(M) LOAD_AS_FILE(M)
  2. If X/index.js is a file, load X/index.js as JavaScript text. 如果X / index.js是文件,则将X / index.js加载为JavaScript文本。 STOP
  3. If X/index.node is a file, load X/index.node as binary addon. 如果X / index.node是文件,则将X / index.node加载为二进制插件。 STOP

So, if a directory has an index.js, it will load that. 因此,如果目录具有index.js,它将加载该目录。

Now, look @ expressjs 现在,看看@ expressjs

http://expressjs.com/guide.html http://expressjs.com/guide.html

create : myapp/routes 创建:myapp / routes

create : myapp/routes/index.js 创建:myapp / routes / index.js

Taking some time to really read how modules work is time well spent. 花一些时间来真正阅读模块是如何工作的,就是花了很多时间。 Read here 在这里阅读

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

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