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!

11
  • Where did you download common.js? Commented Jan 26, 2021 at 18:56
  • look here, possible duplicate: https://stackoverflow.com/a/19059825/5427820 Commented Jan 26, 2021 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. Commented Jan 26, 2021 at 19:05
  • CommonJS is a Node.js module, maybe you are refering to RequireJS? Commented Jan 26, 2021 at 19:59
  • @Camilo I downloaded it as an npm package. Commented Jan 26, 2021 at 20:02

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

-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

2 Comments

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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.