javascript require doesn't work inside html

后端 未结 2 813
孤独总比滥情好
孤独总比滥情好 2021-01-18 13:05

I\'m writing html code that involves js code. Below a simple example:




Use JavaScript to Cha

相关标签:
2条回答
  • 2021-01-18 13:38

    The reason you are getting ReferenceError: require is not defined is because nowhere in your html page is Require included. Require does not come with your standard JavaScript library. You must include the file on your page so it can be loaded and used.

    This can be done by simply adding <script src="myJS.js"></script> in the <head> or <body> tags. The myJS.js file will, of course, be replaced by the require.js file.

    The reason it works when you run with node is because Node already has its own module loader.

    0 讨论(0)
  • 2021-01-18 13:45

    Javascript is not the same as node.js

    require() is not a part of JavaScript standard and is not supported by browsers out of the box, it is the node.js module system.

    You might need to directly include the modules; some of the modules might not work in the browser sandbox context.

    Also, tools such as http://browserify.org/ might be useful.

    0 讨论(0)
提交回复
热议问题