0

I m new in javascript and web developer, i want to connect to postgre database, so I use the following code

<!DOCTYPE html>
<html>
<head>
  <label id="Connected" >Waiting...</label>
</head>
  
  <body>
    <button  class="btn btn-success" onclick="connect()">Load Query</button>
  
  </body>
  <script>

    function connect()
    {
      const { Client } = require('pg');
      const client = new Client({
      user: 'postgres',
      host: 'localhost',
      database: 'helloworld',
      password: 'Mypostgres2021',
      port: 5432,
    })
  
    //   client.connect(function(err) {
    //   if (err) throw err;
    //   console.log("Connected!");
    // });

    
      document.getElementById("Connected").innerHTML = "Connected";
    }
  </script>
</html>

when I run the page I get the following error Uncaught "ReferenceError: require is not defined", I am using visual studio code.

I try to solve this error by installing pg from npm code, but I still get the error.

  • Does this answer your question? [Client on Node.js: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-js-uncaught-referenceerror-require-is-not-defined) – Parzh from Ukraine Aug 20 '21 at 08:45
  • `pg` looks like it's a server side / client lib. So even if you use a packager like webpack, it's still not going to work. You need to be doing your postgre stuff server side inside node.js & not inside the browser,.. – Keith Aug 20 '21 at 08:51

1 Answers1

0

Thanky Dima it's work, i realized that the web programming has two side to consider (a server side and client side).