Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
"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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda