Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vuejs require("axios") in .js file, require is not defined

I’m using vuejs and I’m trying to make a GET request to my API. I’m using axios, but I cannot import it, if I use require to import axios i get this error: Uncaught ReferenceError: require is not defined.

This is my vuetest.js file:

const axios = require("axios");

new Vue({
  el: "#rentalsVue",
  data() {
    return {
      info: null,
    };
  },
  mounted() {
    axios.get("/getRentals").then((response) => (this.info = response));
  },
});

This is the html:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <div id="rentalsVue">
        {{ info }}
    </div>


    <script src="vuetest.js"></script>
</body>

</html>

>Solution :

This happens because require() doesn’t exist on browser/client-side JavaScript.

You need to import Axios using the <script> tag and then you will be able to use it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading