Clean Up Your Project with Modules | Modules should be present error

Tell us what’s happening:
What is missing in my code? I’m getting “Modules should be present.”

server.js

'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const passport = require('passport');
const routes = require('./routes.js');
const auth = require('./auth.js');
const app = express();

fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.set('view engine', 'pug');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
    secret: process.env.SESSION_SECRET,
    resave: true,
    saveUninitialized: true,
    cookie: { secure: false }
}));
app.use(passport.initialize());
app.use(passport.session());

myDB(async client => {
    const db = await client
        .db('database')
        .collection('users');

    routes(app, db);
    auth(app, db);

}).catch(e => {
    app.route('/').get((req, res) => {
        res.render('pug',
            {
                title: e,
                message: 'Unable to login'
            });
    });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log('Listening on port ' + PORT);
});

routes.js

const passport = require('passport');
const bcrypt = require('bcrypt');

module.exports = (app, db) => {
    const ensureAuthenticated = (req, res, next) => {
        if (req.isAuthenticated())
            return next();
        res.redirect('/');
    };

    app.route('/').get((req, res) => {
        res.render('pug', {
            title: 'Connected to Database',
            message: 'Please login',
            showLogin: true,
            showRegistration: true
        });
    });

    app.route('/login').post(
        passport.authenticate(
            'local', {
                failureRedirect: '/'
            }),
        (req, res) => {
            res.redirect('/profile');
        }
    );

    app.route('/profile').get(
        ensureAuthenticated,
        (req, res) => {
            res.render(process.cwd() + '/views/pug/profile', {
                username: req.user.username
            });
        });

    app.route('/logout')
        .get((req, res) => {
            req.logout();
            res.redirect('/');
        });

    app.route('/register')
        .post((req, res, next) => {
            const hash = bcrypt.hashSync(req.body.password, 12);
            db.findOne({ username: req.body.username }, (err, user) => {
                if (err)
                    next(err);
                else if (user)
                    res.redirect('/');
                else
                    db.insertOne({
                        username: req.body.username,
                        password: hash
                    },
                        (err, doc) => {
                            if (err)
                                res.redirect('/');
                            else
                                next(null, doc.ops[0]);
                        }
                    )
            })
        },
            passport.authenticate('local', { failureRedirect: '/' }),
            (req, res, next) => {
                res.redirect('/profile');
            }
        );


    app.use((req, res, next) => {
        res.status(404)
            .type('text')
            .send('Not Found');
    });
};

auth.js

const passport = require('passport');
const LocalStrategy = require('passport-local');
const bcrypt = require('bcrypt');
const ObjectID = require('mongodb').ObjectID;

module.exports = (app, db) => {
    passport.serializeUser((user, done) => {
        done(null, user._id);
    });

    passport.deserializeUser((id, done) => {
        db.findOne(
            {
                _id: new ObjectID(id)
            }, (err, doc) => {
                if (err)
                    return done(err, null);
                done(null, doc);
            });
    });

    passport.use(new LocalStrategy(
        function(username, password, done) {
            db.findOne(
                {
                    username: username
                }, (err, user) => {
                    console.log('User '
                        + username
                        + ' attempted to log in.');
                    if (err)
                        return done(err);
                    if (!user)
                        return done(null, false);
                    if (!bcrypt.compareSync(password, user.password))
                        return done(null, false);
                    return done(null, user);
                });
        }
    ));
};

Your project link(s)

solution: https://replit.com/@ahmedkhfagy/boilerplate-advancednode

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36.

Challenge: Clean Up Your Project with Modules

Link to the challenge:

The issue appears to be with the tests not being able to accept .db on a new line. Changing this to:

await client.db('database')

Should work.

I will add this to an issue with this challenge.

Hope this helps

1 Like

Thanks! this was the issue and it passed the tests right after that.
But why didn’t it accept it like that while it was working perfectly fine on Replit?

Unfortunately, the freeCodeCamp tests use Regex for many of the challenge user stories. This is obviously quite finicky, and causes hiccups like this a lot of the time.

Ideally, the tests and challenges would be written/structured in a way so as to not require testing actual student-written text, but the overall result. This is difficult, though.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.