admin管理员组文章数量:1327743
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');
and that works.
now on my routes.js
I tried to export require('./controllers');
.
But it keeps telling me that it cannot find module controllers
.
Doing this works:
require('./controllers/users')
But I am trying to follow expressjs sample initial projects format.
And it is bugging me because the example of expressjs
shows: (express routes is a folder)
var routes = require('./routes');
and loads it like
app.get('/', routes.index);
with no error. and that ./routes
is a folder. I am just following the same principle.
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');
and that works.
now on my routes.js
I tried to export require('./controllers');
.
But it keeps telling me that it cannot find module controllers
.
Doing this works:
require('./controllers/users')
But I am trying to follow expressjs sample initial projects format.
And it is bugging me because the example of expressjs
shows: (express routes is a folder)
var routes = require('./routes');
and loads it like
app.get('/', routes.index);
with no error. and that ./routes
is a folder. I am just following the same principle.
- 1 nodejs/api/modules.html – bryanmac Commented Feb 22, 2014 at 2:51
3 Answers
Reset to default 4If you try to require
a directory, it expects there to be an index.js
file in that directory. Otherwise, you have to simply require individual files: require('./controllers/users')
. Alternatively, you can create an index.js
file in the controllers
directory and add the following:
module.exports.users = require('./users');
module.exports.posts = require('./posts');
and then import: var c = require('./controllers');
. You can then use them via c.users
and c.posts
.
You have to understand how require() works.
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. 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/api/modules.html
LOAD_AS_DIRECTORY(X)
- If X/package.json is a file, a. Parse X/package.json, and look for "main" field. b. let M = X + (json main field) c. LOAD_AS_FILE(M)
- If X/index.js is a file, load X/index.js as JavaScript text. STOP
- If X/index.node is a file, load X/index.node as binary addon. STOP
So, if a directory has an index.js, it will load that.
Now, look @ expressjs
http://expressjs./guide.html
create : myapp/routes
create : myapp/routes/index.js
Taking some time to really read how modules work is time well spent. Read here
本文标签: javascriptNodeJS cannot find custom moduleStack Overflow
版权声明:本文标题:javascript - NodeJS cannot find custom module - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742226226a2436367.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论