Cesar
Cesar

Reputation: 447

Cannot find module express

Im trying to run an express application and im getting the following error:

module.js:471 throw err; ^

Error: Cannot find module './controllers'

this is my app.js:

const express = require('express');
//let passport = require('passport');
//let session = require('express-session');
const app = express();


app.use(express.static(__dirname + '/public'));
app.use(express.json());
app.use(express.urlencoded({extended:false}));

app.get('/', (req, res) => {
  res.redirect('index.html');
});

app.post('/', (req, res) => {
})

app.use('/',require('./controllers'));

/*
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
*/
app.listen(3000);

I would like to know why im getting this error

this is my application structure:

Structure

Upvotes: 0

Views: 437

Answers (1)

Steve Holgado
Steve Holgado

Reputation: 12071

When you require a directory like this:

app.use('/', require('./controllers'));

...you are technically looking for ./controllers/index.js:

You would have to add the file name, for example:

app.use('/', require('./controllers/route'));

Upvotes: 1

Related Questions