skip to Main Content

I’m following this MERN stack tutorial and stuck on this video about models and schemas, starting around minute 7: https://www.youtube.com/watch?v=O8IipcpTmYU&list=PL4cUxeGkcC9iJ_KkrkBZWZRHVwnzLIoUE&index=5

I created a schema with mongoose, exported, and when requiring it in my routes file it can’t be found.

File structure:

MERN
| client/
| models/
|  - workoutModel.js
| server/
|  - node_modules/
|  - routes/
|  -- workouts.js
|  - .env
|  - package-lock.json
|  - package.json
|  - server.js

workouts.js

const express = require('express');
const router = express.Router();
const Workout = require('../models/workoutModel.js');

// /api/workouts/
// GET all workouts
router.get('/', (req, res) => {
    res.json({msg: 'GET all workouts'})
})

// GET single workout
router.get('/:id', (req, res) => {
    res.json({msg: 'GET single workout'})
})

// POST new workout
router.post('/', async (req, res) => {
     
})

// DELETE a workout
router.delete('/:id', (req, res) => {
    res.json({msg: 'DELETE a workout'})
})

// UPDATE a workout
router.patch('/:id', (req, res) => {
    res.json({msg: 'UPDATE a workout'})
})

module.exports = router;

workoutModel.js

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const workoutSchema = new Schema({
    title: {
        type: String,
        required: true
    },
    reps: {
        type: Number,
        required: true
    },
    load: {
        type: Number,
        required: true
    }
}, { timestamps: true });

module.exports = mongoose.model('Workout', workoutSchema);

Here is my error:

  throw err;
  ^

Error: Cannot find module '../models/workoutModel.js'
Require stack:
- C:UsersUSERDocumentsMERNserverroutesworkouts.js
- C:UsersUSERDocumentsMERNserverserver.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:UsersUSERDocumentsMERNserverroutesworkouts.js:3:17)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:UsersUSERDocumentsMERNserverserver.js:11:23)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\Users\USER\Documents\MERN\server\routes\workouts.js',
    'C:\Users\USER\Documents\MERN\server\server.js'
  ]
}

I’ve followed the tutorial word for word, and I have no idea what is wrong! Any help is appreciated.

2

Answers


  1. maybe this code would be like this

    const Workout = require('../../models/workoutModel.js');
    

    You should check the file path again

    Login or Signup to reply.
  2. Can you try once with this line?

    const Workout = require('../models/workoutModel');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search