Search code examples
javascripthtmlnode.jsrequirejs

Uncaught Error: Module name "https" has not been loaded yet for context: _. Use require([])


This is the error that I'm getting-

Uncaught Error: Module name "https" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
    at makeError (require.js:168)
    at Object.localRequire [as require] (require.js:1436)
    at requirejs (require.js:1797)
    at Dataverse_API_node.js:1

I have an explore.html page which calls the script Dataverse_API_node.js, which is itself dependent on the require.js file. My Dataverse_API_node.js script is supposed to display its output data on the explore.html page. However, I face the above error when I load the explore.html page.

Note that my Dataverse_API_node.js script runs independently without errors and displays the data in the command prompt when run through the Node.js command prompt.

There are the first few lines of the Dataverse_API_node.js script-

const https = require("https");

var parseString = require('xml2js').parseString;

const demo_url = "https://demo.dataverse.org"

const actual_url = "https://demo.dataverse.org/dataverse/hbstest"

const search_uri = "/api/search"

const options = "?q=*&subtree=COSgak"

This is the part of explore.html where I'm calling the Dataverse_API_node.js script and supposed to display the data-

<p>
   <script src="Dataverse_API_node.js" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js">
        document.write(search_for_all("https://demo.dataverse.org", 0, 'file'));
   </script>
</p>

I'm new to Javascript and would really appreciate some help with this error. Please don't send me to requirejs.org as it doesn't really help.

Thank you for your time.


Solution

  • Solution found. Just use browserify. Install browserify and then run the following command-

    > browserify Dataverse_API_node.js > bundle.js
    

    Browserify goes through all the 'require' in your script and wraps it up in bundle.js and helps to run the node.js modules in the browser.

    Just add the following script in your .html file-

    <script type=text/javascript src="scripts/bundle.js"></script>