Uncaught ReferenceError: module/require is not defined

感情迁移 提交于 2021-02-17 05:47:45

问题


I am learning about how to create/import/export modules in Node.js, i have gone through these and was trying to learn by creating a sample application. I issued the command from the root folder (Poc1) "npm install requirejs", and included the file require.js in the Start.html.

When i open Start.html in browser i get -

"Uncaught ReferenceError: require is not defined", "Uncaught ReferenceError: module is not defined".

I am not sure what mistake i am making or what other files i need to include to get it working ?

For sample application below is folder structure

Poc1 folder has these files and folders (greetings.js, main.js, Start.html, node_modules)
Poc1\node_modules has (requirejs\require.js)

grreetings.js is defined as below

    module.exports.sayHelloInEnglish = function(){
        return "Hello";
    }

    module.exports.sayHelloInSpanish = function(){
        return "Hola";
    }

main.js is defined as below

    var greetings = require("./greetings.js");
    function someFunc(){
        var g1 = greetings.sayHelloInEnglish();
        var g2 = greetings.sayHelloInSpanish();

        alert(g1);
        alert(g2);
    }

Start.html is defined as below

<html>
<head>
  <script type="text/javascript" src="main.js"></script>
  <script type="text/javascript" src="greetings.js"></script>
  <script type="text/javascript" src="node_modules\requirejs\require.js"></script>
</head>
<body>
  <span>Below is button</span>
  <input type="button" onclick="someFunc()" value="clickMe"/>
</body>
</html>

回答1:


Ok, I think those exports aren't correct:

exports.sayHelloInEnglish = function(){
    return "Hello";
}

exports.sayHelloInSpanish = function(){
    return "Hola";
}


来源:https://stackoverflow.com/questions/32816813/uncaught-referenceerror-module-require-is-not-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!