Daniel
Daniel

Reputation: 6491

Axios with node.js - ReferenceError: XMLHttpRequest is not defined

I'm firing a GET request from a simply configured axios instance:

var Axios = axios.create({
        baseURL: myBaseUrl,
        headers: {'content-type': 'application/json'}
    });

Axios.get(url)

And been getting the following error:

ReferenceError: XMLHttpRequest is not defined
    at dispatchXhrRequest (C:\...\node_modules\axios\dist\axios.js:804:24)
    at xhrAdapter (C:\...\node_modules\axios\dist\axios.js:796:11)
    at process._tickCallback (internal/process/next_tick.js:103:7)
    at Function.Module.runMain (module.js:577:11)
    at startup (node.js:160:18)
    at node.js:456:3

To my understanding (and according to the docs), axios should work on both web browsers and node.js.

What am i missing?

Versions:

node - 6.2.1
axios - 0.13.1

Upvotes: 4

Views: 11162

Answers (1)

Molda
Molda

Reputation: 5704

It looks like you are requiring a browser-side file and this line node_modules\axios\dist\axios.js confirms it.

It should be node_modules/axios/lib/axios.js

Update:

for node.js use

var axios = require('axios');

and for browser simply add a script tag and axios object should be globally available

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

or link your local axios.min.js file for example

<script src="/node_modules/axios/dist/axios.min.js"></script>

Upvotes: 5

Related Questions