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>
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