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 Express Basics Parameters, Query Strings, and Modularizing Routes Modular Routes

Juan Herrera
seal-mask
.a{fill-rule:evenodd;}techdegree
Juan Herrera
Full Stack JavaScript Techdegree Student 13,649 Points

I got an error, Error: Cannot find module './routes' after running nodemon

I got this error on the console:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module './routes'
Require stack:
-~/Express Basics/flashcards/app.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> (~/Express Basics/flashcards/app.js:11:16)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '~/Express Basics/flashcards/app.js'       
  ]
}
[nodemon] app crashed - waiting for file changes before starting...

This is my code:

App.js

const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');

const app = express();
app.use(bodyParser.urlencoded({extended:false}));
app.use(cookieParser());

app.set('view engine', 'pug');

const routes = require('./routes');

app.use(routes);

app.use((req, res, next) => {
    console.log("World");
    next();
});

app.use((req, res, next) => {
    const err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handler
app.use((err, req, res, next) => {
    res.locals.error = err;
    res.status(err.status);
    res.render('error', err);
});

app.listen(3000, () => {
    console.log('The application is running on localhost:3000!')
});

index.js

const express = require('express');
const router = express.Router();

// Root Route
router.get('/', (req, res) => {
    const name = req.cookies.username;    
    if (name) {
        res.render('index', {name});
    } else {
        res.redirect('/hello');
    }    
});

// Cards route
router.get('/cards', (req, res) => {
    res.render('card', {prompt: "Who is buried in Grant's tomb?"});
});

// Hello route
router.get('/hello', (req, res) => {
    const name = req.cookies.username;
    if (name) {
        res.redirect('/');
    } else {
        res.render('hello');
    }
});

// Hello Post 
router.post('/hello', (req, res) => {
    res.cookie('username', req.body.username);
    res.redirect('/');
});

// Goodbye Route
router.post('/goodbye', (req, res) => {
    res.clearCookie('username');
    res.redirect('/hello');
});

module.exports = router;

2 Answers

Marcio Gessoni
Marcio Gessoni
14,941 Points

I got this same error: Error: Cannot find module './routes'

To fix it I realized that I had to put the routes folder at the same level as view and not inside it. Problem solved.