Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
"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.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda