title: cannot find module '../app/models/user'
body: <p>im struggled with this problem like more then 40 hours and sill don't know how to solve this, the problem is a little big to explain but i will try my best, im a little newbie with Node.Js and mongo stuffs so forgive me any stupid mistake.</p>
  
  <p>I followed the book <strong>Mean Stack</strong> and the problem appears when i started <a href="https://scotch.io/tutorials/easy-node-authentication-setup-and-local" rel="nofollow noreferrer">this</a> tutorial to do the autentication with auth, i am at the beginning doing the local authentication without any social at the moment.</p>
  
  <p><strong>folder structure</strong></p>
  
  <p><a href="https://i.stack.imgur.com/a7ttT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a7ttT.png" alt="folder structure"></a></p>
  
  <p><strong>so basicly i set the server like this:</strong></p>
  
  <pre><code>process.env.NODE_ENV = process.env.NODE_ENV || 'development';
  var mongoose = require('./config/mongoose');
  var express = require('./config/express');
  
  
  var db = mongoose();
  var app = express();
  app.listen(3000);
  
  module.exports = app;
  
  console.log("running at port 3000");
  </code></pre>
  
  <p><strong>this is the ./config/mongoose</strong></p>
  
  <pre><code>var config = require('./config');
  var mongoose = require('mongoose');
  var connectionString = "mongodb://localhost:27017/ShareIdea"
  
  module.exports = function(){
  mongoose.Promise = global.Promise;
  var db = mongoose.connect(connectionString);
  
  require('../app/models/user.server.model');
  
  return db;
  </code></pre>
  
  <p>};</p>
  
  <p><strong>the required user.server model</strong></p>
  
  <pre><code>var mongoose = require('mongoose');
  var bcrypt   = require('bcrypt-nodejs');
  var Schema = mongoose.Schema;
  
  var UserSchema = new Schema({
      local            : {
          email        : String,
          password     : String,
      },
      facebook         : {
          id           : String,
          token        : String,
          email        : String,
          name         : String
      },
      twitter          : {
          id           : String,
          token        : String,
          displayName  : String,
          username     : String
      },
      google           : {
          id           : String,
          token        : String,
          email        : String,
          name         : String
      }
      /*firstname:String,
      lastname:String,
      email:String,
      username:String,
      password:String,
      userChoice: {type: String, possibleValues: ['programmer','inovator']}*/
  });
  
  UserSchema.methods.generateHash = function(password) {
      return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
  };
  
  UserSchema.methods.validPassword = function(password) {
      return bcrypt.compareSync(password, this.local.password);
  };
  
  mongoose.model('User',UserSchema);
  </code></pre>
  
  <p><strong>here is the express configuration</strong></p>
  
  <pre><code>var config          = require('./config');
  var express         = require('express');
  var passport        = require('passport');
  var flash           = require('connect-flash');
  var compress        = require('compression');
  var cookieParser    = require('cookie-parser');
  var session         = require('express-session');
  var bodyParser      = require('body-parser');
  var morgan          = require('morgan');
  var methodOverride  = require('method-override');
  var expressLayouts  = require('express-ejs-layouts');
  
  module.exports = function(){
      var app = express();
  
      **require('./passport')(passport);**
  
      if(process.env.NODE_ENV === 'development')
      {
          app.use(morgan('dev'));
      }  
      else if(process.env.NODE_ENV === 'production')
      {
          app.use(compress());
  
      }
      app.use(cookieParser());
  
      app.use(bodyParser.urlencoded({
         extended:true 
      }));
      app.use(morgan('dev'));
  
      app.use(bodyParser.json());
      app.use(methodOverride());
  
      app.use(session({
          saveUninitialized: true,
          resave: true,
          secret: 'aaaaaaa'
      }));
  
  
  
      app.use(passport.initialize());
      app.use(passport.session()); // persistent login sessions
      app.use(flash());
  
      app.set('views', './app/views');
      app.set('view engine','ejs');
      app.use(expressLayouts);
  
  
  
      require('../app/routes/index.server.routes.js')(app,passport);
      require('../app/routes/register.server.routes.js')(app,passport);
      require('../app/routes/login.server.routes.js')(app,passport);
      require('../app/routes/profile.server.routes.js')(app,passport);
      app.use(express.static('./public'));
      return app;
  }
  </code></pre>
  
  <p>here in the express is where the problems appear, this line is the main problem at the moment: `require('./passport')(passport);</p>
  
  <p>if i put this code it gives me the problem that i mentioned in the title, if not when i do a POST request with my form it never posts, i have this troubles because i set the structures of my code to a MVC structure like the book and want to adapt the authentication in the tutorial to my code to learn a little, </p>
  
  <p><strong>so basicly im passing the passport to my routes like this:`</strong></p>
  
  <p><strong>register route</strong></p>
  
  <pre><code>var register = require('../../app/controllers/register.server.controller');
  
  module.exports = function(app,passport) {
   app.route('/register')
   .post(function(req,res){
       console.log("HY");
        passport.authenticate('local-signup', {
          successRedirect : '/profile', // redirect to the secure profile section
          failureRedirect : '/register', // redirect back to the signup page if there is an error
          failureFlash : true // allow flash messages
    })})
   .get(register.getPage);
  };
  </code></pre>
  
  <p><strong>Register controller</strong></p>
  
  <pre><code>var User = require('mongoose').model('User');
  
  module.exports = {
  
    getPage: function(req,res){
        res.render('./pages/register',{ message: req.flash('signupMessage') });
    }
  
  };
  </code></pre>
  
  <p><strong>and to end here is the passport code:</strong></p>
  
  <pre><code>// config/passport.js
  
  // load all the things we need
  var LocalStrategy   = require('passport-local').Strategy;
  
  // load up the user model
  var User            = require('../app/models/user');
  
  // expose this function to our app using module.exports
  module.exports = function(passport) {
  
      // =========================================================================
      // passport session setup ==================================================
      // =========================================================================
      // required for persistent login sessions
      // passport needs ability to serialize and unserialize users out of session
  
      // used to serialize the user for the session
      passport.serializeUser(function(user, done) {
          done(null, user.id);
      });
  
      // used to deserialize the user
      passport.deserializeUser(function(id, done) {
          User.findById(id, function(err, user) {
              done(err, user);
          });
      });
  
      // =========================================================================
      // LOCAL SIGNUP ============================================================
      // =========================================================================
      // we are using named strategies since we have one for login and one for signup
      // by default, if there was no name, it would just be called 'local'
  
      passport.use('local-signup', new LocalStrategy({
          // by default, local strategy uses username and password, we will override with email
          usernameField : 'email',
          passwordField : 'password',
          passReqToCallback : true // allows us to pass back the entire request to the callback
      },
      function(req, email, password, done) {
  
          // asynchronous
          // User.findOne wont fire unless data is sent back
          process.nextTick(function() {
  
          // find a user whose email is the same as the forms email
          // we are checking to see if the user trying to login already exists
          User.findOne({ 'local.email' :  email }, function(err, user) {
              // if there are any errors, return the error
              if (err)
                  return done(err);
  
              // check to see if theres already a user with that email
              if (user) {
                  return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
              } else {
  
                  // if there is no user with that email
                  // create the user
                  var newUser            = new User();
  
                  // set the user's local credentials
                  newUser.local.email    = email;
                  newUser.local.password = newUser.generateHash(password);
  
                  // save the user
                  newUser.save(function(err) {
                      if (err)
                          throw err;
                      return done(null, newUser);
                  });
              }
  
          });    
  
          });
  
      }));
  
  };
  </code></pre>
  
  <p>Ps: Sorry for the long post, i hope i can get a explanation since i want to learn more, when i read the book i felt like the mvc structure adapted by the autor was dificult, but anyways thanks a lot.</p>
  
  <p>if you guys want to not look at this i understand and here is the gitProject:<a href="https://github.com/FilipeCosta/MeanStackBook" rel="nofollow noreferrer">git project</a> </p>