Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
"ReferenceError: require is not defined" when importing a js file

This is an old question that has been asked many times. I have tried all the solutions but nothing works.

I am using MongDB and want the names of users stored in the database to be displayed on the main page as a list even after closing and reopening the browser. When I import phones.js in code.js, I get :

ReferenceError: require is not defined

Thus, I can't use the database to display the names. I understand that browsers don't have the require method defined, Node.js does. But is there a turnaround to this?

app.js is in main directory ( . / )

const mongoose = require('mongoose');
const Phones = require('./models/phones');
const { ObjectId } = require('mongodb');

//...MongoDB connection

app.use(express.static(path.join(__dirname, 'public')));

//...creating endpoints

phones.js is in ./models/phones.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

//...create Schema

const Phones = mongoose.model('Phones', phonesSchema);
module.exports = Phones; 

code.js in ./public/code.js

const Phones = require('../models/phones.js');
console.log(Phones.find({}));

//...create an 'li' for each number and append it to 'ul'

index.html in ./public/index.html

<script src="code.js" ></script>

I have used browserify : browserify app.js -r bundle.js and I get a huge error.

If there's another approach to display on a page the names stored in the database even after closing the browser it will be accepted as the solution also.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!