Reference Error, Require is not defined

I am trying to connect to the MongoDB server locally in a js file.
My code so far:

var MongoClient = require("mongodb").MongoClient;

function registerUser() {
  const firstName = $("#firstName").val();
  const lastName = $("#lastName").val();
  const emailAddress = $("#emailAddress").val();
  const cnic = $("#cnic").val();
  const password = $("#password").val();
  const address = $("#address").val();

  var url = "mongodb://localhost:27017/";

  MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    var dbo = db.db("library_management_system");
    var myobj = {
      firstName: firstName,
      lastName: lastName,
      email: emailAddress,
      cnic: cnic,
      password: password,
      address: address
    };
    dbo.collection("users").insertOne(myobj, function(err, res) {
      if (err) throw err;
      console.log("1 document inserted");
      db.close();
    });
  });
}
// registerUser();
$(".registration-form").submit(function(e) {
  return false;
});

$("#btn-register").on("click", registerUser);

When I open up my page in the browser, the chrome console gives an error:

registerDB.js:1 Uncaught ReferenceError: require is not defined
at registerDB.js:1
(anonymous) @ registerDB.js:1

I will really appreciate it if somebody tries to resolve this issue.

Are you running Node and are you trying to connect via client side or server side.

1 Like

I have an html page which is connected to this database page,( I posted the code), I just open the html page in browser and tried to get form field values through jquery and sending them to mongodb server using node drivers for mongodb. That’s all i know.

Require is not implemented into the browser, its specific to Node. So you can use it on the serve side within the Node environment but not on the client. You can look into something like require.js to help you or you will have to bring it in another way.

1 Like

Is there any way, to use it on client side?

Require.js is one option or Browserify is another:

Brother , sorry to say, I already know about these options. But i don’t see how to use browserify in my project. Can you please guide me?

Is there any command to install browserify in my project. and then how to use it?

In the links above you can look through the README file and it shows you how to install the packages and use them

I have installed it using

npm install -g browserify

but the file has a lot of information. How can I use it specifically for require function?

I am also going through the same issue and I am new to js. can you please help on how you worked with browserify?
Thanks

Browserify bundles all of your JavaScript into one file, which you include in your html.

http://browserify.org/ has the instructions:

  1. require your JS as you normally would.
  2. npm install any libraries you need.
  3. use the ‘browserify’ command to create one mega-file (assuming you call your main js file ‘main.js’):
    browserify main.js -o bundle.js
  4. include the bundled file in your html:

I was also stuck in this for quite some time. Now figured it out :slight_smile:

  1. Problem :
    require() is not a feature that is built into the browser. It is a specific feature of node.js, not of a browser. So, when you try to have the browser run your script, it does not have require()

  2. Solution :
    To do so you can use tools like browserify or webpack. These tools are module bundler. And their main purpose is to bundle JavaScript files for usage in a browser. I’ll give an example of using browserify as I found it easier.

  3. Steps :

a) Install browserify using

$ npm install -g browserify

b) Now just use the browserify command to build a bundle starting at main.js :

$ browserify main.js > bundle.js

c) All of the modules that main.js needs are included in the bundle.js

  • To use this bundle, just toss a
    <script src="bundle.js"></script> into your html!