ReferenceError: mongoose is not defined

13,519

You need to install mongoose as a dependency

run this command,

$ npm install mongoose --save

also replace

var app = require('mongoose');

to

var mongoose = require('mongoose');
Share:
13,519
Tesd te
Author by

Tesd te

Updated on June 06, 2022

Comments

  • Tesd te
    Tesd te almost 2 years

    I am getting ReferenceError: mongoose is not defined. I am new to node please App.js code is below is as shown below. I am not able to solve it by all the answers provided in google.

       var express = require('express');
     var app = require('mongoose');
     var path = require('path');
     var favicon = require('serve-favicon');
    var logger = require('morgan');
    var cookieParser = require('cookie-parser'); 
    var bodyParser = require('body-parser');
    
     var index = require('./routes/index');
    var users = require('./routes/users');
    var app = express();
    app.set('views', path.join(__dirname, 'views')); 
       app.set('view engine', 'jade');
    
     app.use(logger('dev'));
      app.use(bodyParser.json());
      app.use(bodyParser.urlencoded({ extended: false }));
       app.use(cookieParser());
     app.use(express.static(path.join(__dirname, 'public')));
    
     app.use('/', index);
     app.use('/users', users);
     app.use(function(req, res, next) {
     var err = new Error('Not Found');
      err.status = 404;
      next(err);
      });
    
      mongoose.connect('mongodb://localhost:27017/mydb');
    
      var Schema = new mongoose.Schema({
      cr   : String,
    
            });
    
           var user = mongoose.model('myc', Schema);
    
        app.get('/view', function(req, res){
         user.find({}, function(err, docs){
          if(err) res.json(err);
        else    res.render('index', {users: docs});
          });
                 });
    
    
          app.use(function(err, req, res, next) {
    
        res.locals.message = err.message;
         res.locals.error = req.app.get('env') === 'development' ? err : {};
    
        res.status(err.status || 500);
       res.render('error');
      });
    
     module.exports = app;
    

    I am using node version6.11.0 and npm v 5.5.1 How to resolve this? pleas help

  • Tesd te
    Tesd te over 6 years
    Its installed via npm only
  • Hunter
    Hunter over 6 years
    True and I should have been more clear. Though the answer stands and is obviously accurate, though I was beat by 3 seconds by @Sajeetharan
  • Sweta Jain
    Sweta Jain over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review