Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Building a MEAN Application Going MEAN with Express Mocking Data

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

Error: Cannot find module '../../mock/todos.json'

For some reason it can't find my json file...

https://github.com/bwhiting2356/mean-todo

todos.json

[
    {"name": "clean the house"}, 
    {"name": "water the dog"}, 
    {"name": "feed the lawn"},
    {"name": "pay dem bills"},
    {"name": "run"}, 
    {"name": "swim"}
]

index.js

'use strict';

var express = require('express');

var todos = require('../../mock/todos.json');

var router = express.Router();

router.get('/todos', function(req, res) {
    res.json({todos: todos});
})

// TODO: Add POST route to create new entries

// TODO: Add PUT route to update existing entries

// TODO: Add DELETE route to delete entries

module.exports = router;
Brendans-MacBook-Pro:mean-todo brendan.whiting$ nodemon
[nodemon] 1.9.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node src/app.js`
module.js:327
    throw err;
    ^

Error: Cannot find module '../../mock/todos.json'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/Users/brendan.whiting/Projects/mean-todo/src/api/index.js:5:13)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
[nodemon] app crashed - waiting for file changes before starting...
Ken Howard
Ken Howard
Treehouse Guest Teacher

By looking at your repository it doesn't appear to have the "mock" folder at the root of your project. Can you verify that you have a "mock" folder at the root of your project with todos.json in that folder?

Marie Nipper
Marie Nipper
22,638 Points

This is late, but what Ken said. If you pulled the git repository and didn't create your mock like in the video, then your mock folder is in your public. So your todos would include the path to the public folder, then mock.

var todos = require('../../public/mock/todos.json');

2 Answers

Dryden Williams
Dryden Williams
4,223 Points

Hey Ben,

*See Justin's comment above.

If you missed it, change:

var todos = require('../../mock/todos.json');

to (just missed the public off the path):

var todos = require('../../public/mock/todos.json');
Bryant Feld
seal-mask
.a{fill-rule:evenodd;}techdegree
Bryant Feld
Full Stack JavaScript Techdegree Student 26,144 Points

you cannot make a get request for files that are not being served, the express server is set up to serve from "public" so you have to put the mock folder in the public folder and change the path in index.js accordingly, I think this was an uncaught glitch in the course. it does not work if mock is in the root