Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!