Harshal
Harshal

Reputation: 3622

How to import the object from React file to plain vanila javascript file

I need help to find out the way to import the object from react file to plan the vanilla javascript file. The react js file is the config file which is exporting the multiple URLs from that file, just like below:

let API;
module.exports = {
 API: 'https://localhost/3000/general/'
}

I would like to use this API path in the different plan javascript file.

When I use it using the below code,

    import { API } from "../src/config";
function sendRequest(query) {
        var data = JSON.stringify(query);
        var url = API

        // Send Request.
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                 console.log(xhr.response);
            }
        };
        xhr.open("POST", url, true);
        xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
        xhr.send(data);

    }

It is giving me an error : can not use import statement outside a module

I tried with 'require' as well and it is also giving error : Uncaught ReferenceError: require is not defined

Any lead would be appreciated!

Upvotes: 1

Views: 46

Answers (1)

2239559319
2239559319

Reputation: 124

export default {
 API: 'https://localhost/3000/general/'
}
import { API } from "../src/config";

do like this

Upvotes: 1

Related Questions