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

437
Visualizações
Trying to use AXIOS for get request, error "require is not defined"

My code is below. This is my first time using NodeJS and axios.

I have installed NodeJS on Windows using the downloadable installer. I then installed Axios using npm install axios in powershell as admin.

I'm obviously copy/pasting the boilerplate nodejs code for an axios get request.

I've tested the php file using a curl get request from command line (installed actual curl, not the windows shortcut to another program.) So the PHP is accepting get requests and serving json back properly.

The issue I'm stuck on is how to setup axios and node js properly so that my js file runs the get request properly.

This is my code

const loginForm = document.getElementById("login-form");
const loginButton = document.getElementById("login-form-submit");
const loginErrorMsg = document.getElementById("login-error-msg");
const vanillaUrl = "localhost:8081/hospital/php/addapt.php";
const getUrl = "localhost:8081/hospital/php/getpatientappt.php?PatientID=1";

const axios = require('axios')
//get request to get appt for patient id1
axios
    .get(geturl)
    .then(res => {
        console.log(`statusCode: ${res.status}`)
        console.log(res)
        alert(res)
    })
    .catch(error => {
        console.error(error)
    })


loginButton.addEventListener("click", (e) => {
    e.preventDefault();
    const username = loginForm.username.value;
    const password = loginForm.password.value;
    if (username === "user" && password === "1") {
        alert("You logged in!");
        location.reload();
    } else {
        loginErrorMsg.style.opacity = 1;
    }
}
)

These are the errors I'm getting in chrome from the javascript console

Uncaught ReferenceError: require is not defined
    at login-page.js:7:15

DevTools failed to load source map: Could not load content for chrome-extension://hnfanknocfeofbddgcijnmhnfnkdnaad/content.js.map: System error: net::ERR_BLOCKED_BY_CLIENT

DevTools failed to load source map: Could not load content for http://localhost:8081/hospital/login/requestProvider.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE

When I run incognito I get

Uncaught ReferenceError: require is not defined
    at login-page.js:7:15

there is no file requestProvider.js.map or content.js.map

This is all being hosted on localhost with xampp. I'm not sure if that affects anything.

I'm not sure what this error is. Got stuck googling it. And I'm not sure how to integrate this code properly.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

seems like you're running this code in browser, browser implementation of Javascript doesn't have require function

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