Search code examples
node.jsmongodbexpressnodemon

module.js:549 throw err; Error: Cannot find module './models/TodoListModel' Nodemon shows this error on adding model file


While i am trying to add my model file which is ToDoListModel.js in my server.js file following error is shown:--

**module.js:549
    throw err;
    ^**

    **Error: Cannot find module './models/TodoListModel'**
        at Function.Module._resolveFilename (module.js:547:15)
        at Function.Module._load (module.js:474:25)
        at Module.require (module.js:596:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (D:\xampp\htdocs\todolist\server.js:28:1)
        at Module._compile (module.js:652:30)
        at Object.Module._extensions..js (module.js:663:10)
        at Module.load (module.js:565:32)
        at tryModuleLoad (module.js:505:12)
        at Function.Module._load (module.js:497:3)
        at Function.Module.runMain (module.js:693:10)
        at startup (bootstrap_node.js:191:16)
        at bootstrap_node.js:612:3
    [nodemon] app crashed - waiting for file changes before starting

...


Project Folder Structure

when added foenter image description herellowing code in server.js it shows error-

module.exports = mongoose.model('Tasks', TaskSchema);
var Task = mongoose.model('Tasks');

Schema is created in ToDoListModel.js

var mongoose = require('mongoose'); var Schema = mongoose.Schema;

var TaskSchema = new Schema({
    name: {
       type: String,
       require: 'Kindly enter the name of the task'
     },
    Created_date: {
       type: Date,
       default: Date.now
     },
   status: {
      type: [{
          type: String,
          enum: ['pending', 'ongoing', 'completed']
       }],
    default: ['pending']
    }
 });
module.exports = mongoose.model('Tasks', TaskSchema);

Any help will be appricated.


Solution

  • server.js file is unable to locate your TodoListModel.js file. you should give the exact path of your model file in server.js, try this it will work.

        var Tasks = require('./app/models/TodoListModel');