node.jsmodulebackendbrowserifyreferenceerror

Browserify:Can't walk dependency graph: ENOENT: no such file or directory error


Browserify:Can't walk dependency graph: ENOENT: no such file or directory error, facing this issue while im trying to bundle my index.js which contains express and mongoose so that i can avoid browsers require is not defined error here is my index.js script

const express = require('express');
const app = express();
const port = 3000;
const mongoose = require('mongoose');

main().catch(err => console.log(err));

async function main() {
    await mongoose.connect('mongodb://127.0.0.1:27017/contact');
}

const contactSchema = new mongoose.Schema({
    name: String,
    phone: String,
    mail: String,
    desc: String
});

const contact = mongoose.model('contact', contactSchema);

// Uncomment the following line to use body-parser middleware
// const bodyparser = require("body-parser")

app.use(express.urlencoded({ extended: true }));

app.post('/contact', (req, res) => {
    const { name, phone, mail, desc } = req.body;

    const newContact = new contact({
        name,
        phone,
        mail,
        desc
    });

    newContact.save()
        .then(() => {
            res.send("Form submitted successfully");
        })
        .catch(() => {
            res.status(400).send("Unable to submit form");
        });
});

app.use(express.static('public'));

app.listen(port, () => console.log(`Server running on port ${port}`));

screenshot of this error

i previously started learning backend and this problem is causing frustration to me, i looked for the solutions on the internet and youtube and couldn't find it anywhere,any help in solving this problem is highly apreciated🙏🙏🙏


Solution

  • Before you read

    Node and Express apps run on a machine/computer not inside a browser. So you have to use npm to run those. Here's a detailed answer that explains everything. As you just started with backend, this might be new to you

    Answer

    I don't think browserify is the right tool to bundle a server side application. It's better to use esbuild.

    Here's a good article I found that shows how to do so.