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

374
Vistas
JS: button click to use custom script which uses axios: require is not defined error

I have a button which, when clicked, I'd like to execute the oof function, which uses axios. However the error occurs:

Uncaught ReferenceError: require is not defined

If I remove everything that has to do with axios from the oof, it would execute (print to console) just fine.

myscript.js:

const {axios} = require("axios");

const oof = () => {
    console.log("HELLO");

    axios.post('http://localhost:3000/oof',
        {message:'hi'})
}

index.html:

  ...
  <div class="oof-button" onclick="oof()">
     oof
  </div>
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I guess you are not using node.js and this causes 'require' undefined.

To use axios in Vanilla JS, you can just use the CDN in HTML. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

For more you can check this article: https://javascript.plainenglish.io/how-to-use-axios-in-vanilla-javascript-2dbf176e08d4

about 4 years ago · Santiago Trujillo Denunciar
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