0

I've been getting this error:

main.js:1 Uncaught ReferenceError: require is not definedat main.js:1

and this one:

format.html:6 GET http://127.0.0.1:5500/common.js net::ERR_ABORTED 404 (Not Found)

I've downloaded Common JS and have included it in my HTML script.

HTML:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src = "main.js"></script>
    <script src="common.js"></script> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
</head>
<body>
    <textarea id = "textArea" rows = "50" cols = "80">
    </textarea>
    <button type = "button">Click me!</button>
</body>
</html>

Javascript:

    const http = require('http');
    const url = require('url');
    const fs = require('fs');
    var browserify = require('browserify');
    var b = browserify();
    b.add('./browser/main.js');
    b.bundle().pipe(process.stdout);

How can I fix this? Thanks!

Apple Pie
  • 9
  • 1
  • 7
  • Where did you download `common.js`? – Camilo Jan 26 '21 at 18:56
  • look here, possible duplicate: [https://stackoverflow.com/a/19059825/5427820](https://stackoverflow.com/a/19059825/5427820) – Kidas Jan 26 '21 at 18:57
  • Why is the title "Node JS Require is not defined" when the code is running in the browser, not Node.js? You can't take code that runs in a Node.js host environment and run it verbatim in a browser host environment. – Heretic Monkey Jan 26 '21 at 19:05
  • CommonJS is a Node.js module, maybe you are refering to RequireJS? – Camilo Jan 26 '21 at 19:59
  • @Camilo I downloaded it as an npm package. – Apple Pie Jan 26 '21 at 20:02
  • @Kidas Thank you but now I realize that and my question aren't the same, so I have to reword my question. – Apple Pie Jan 26 '21 at 20:02
  • @HereticMonkey so how would I modify it to fit the browser host? Do you have any pointers, or is it not that simple? – Apple Pie Jan 26 '21 at 20:03
  • So I'm getting some extra errors here: ```GET http://127.0.0.1:5500/require.js net::ERR_ABORTED 404 (Not Found) GET http://127.0.0.1:5500/common.js net::ERR_ABORTED 404 (Not Found)``` Apparently it's having problems with finding the scripts as well. I recently added require.js to see if it helped. Any ideas? Thanks. – Apple Pie Jan 26 '21 at 20:13
  • [Start a Node.js file/module from JavaScript (browser)](https://stackoverflow.com/q/52289413/215552) There's no "one-size-fits-all" approach. You have to know what the script depends on and only use the language, not things that are Node.js-specific. – Heretic Monkey Jan 26 '21 at 20:21
  • I think your current effort is wasted; you're not going to be able to run the code shown in a browser. – Heretic Monkey Jan 26 '21 at 20:24
  • @HereticMonkey I genuinely just figured that out... thanks for the help everyone. – Apple Pie Jan 26 '21 at 20:31

2 Answers2

0

For any beginners like me who are trying to do this... just run JS and reference it from your HTML script. This(from my understanding) does not work! Node JS will not be compatible!

Apple Pie
  • 9
  • 1
  • 7
-1

Try putting the common.js call before the main.js in the HTML and if that doesn't solve it, try setting the defer attribute on both of them

Gustavo Shigueo
  • 399
  • 3
  • 11
  • Ah sadly it didn't work. Not sure what to do now. I now realize that since I'm running this code in a browser, it's not the same. Any ideas on how to define require in my browser, since it's different from a Node.js host environment? – Apple Pie Jan 26 '21 at 20:04
  • Check your package.json and other node configuration files to see if any of them has `"type": "module"`, if so, change it to `"type": common"` or delete the line – Gustavo Shigueo Jan 26 '21 at 20:08