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

242
Visualizações
Uncaught ReferenceError: require is not defined in browser

when i run the js file where this function is written it works and show data in the console. but when i run it in a browser from the html file with onclick i get the error: Uncaught ReferenceError: require is not defined i tried using the function in tag but didn't work

function appendData() {
  const data = require("../assets/Data/Courses.json");
    
  var length = Object.keys(data.employees).length;
    var Text = new Array();
    for(let i = 0; i < length; i++) 
    {
        //document.getElementById('test').innerHTML += "<br>"+data.employees[i].firstName + " " + data.employees[i].lastName ;
        Text[i] = data.employees[i].firstName+ " " + data.employees[i].lastName ;
        console.log(Text[i]);
    } 
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

require() is actually Node style of importing code. So it won't work in this case. I don't know your setup but if you are using vanilla JS (without babel setup), you can include .json files to your code by using the fetch API

fetch("../assets/Data/Courses.json")
  .then(response => { 
      const data = response.json();
      var length = Object.keys(data.employees).length;
    var Text = new Array();
    for(let i = 0; i < length; i++) 
    {
        //document.getElementById('test').innerHTML += "<br>"+data.employees[i].firstName + " " + data.employees[i].lastName ;
        Text[i] = data.employees[i].firstName+ " " + data.employees[i].lastName ;
        console.log(Text[i]);
    } 
    })
  .then(data => console.log(data))
  .catch(error => console.log(error));
about 4 years ago · Juan Pablo Isaza Relatório
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