How to connect a Postgres Database to a HTML page with Node.js?

First thing first: I know basically nothing about JS 🙂 (coming from a Java background)

I have [email protected] and [email protected] installed.

I am getting the following error message:
“Uncaught TypeError: Failed to resolve module specifier “pg”. Relative references must start with either “/”, “./”, or “../”.”

(probably some CommonJS vs ECMAscript problem whatever that means)

As the title says I am trying to connect to a Postgres database with the following codes:
postgres.js (the first line is just for seeing if it actually runs, which it does not)

    console.log("hello POSTgres");
    
    import { Client } from "pg";
    require("dotenv").config();
    
    export async function getClient() {
      const client = new Client({
        host: process.env.PG_HOST,
        port: process.env.PG_PORT,
        user: process.env.PG_USER,
        password: process.env.PG_PASSWORD,
        database: process.env.PG_DATABASE,
        ssl: true,
      });
      await client.connect();
      return client;
    }

In the html its referenced like this

    <script type="module" src="postgres.js"></script>

package.json

  "type": "module",
  "dependencies": {
    "dotenv": "^16.0.3",
    "pg": "^8.9.0"
  }
}

variables.env

PG_PORT=<5432>
PG_USER=<postgres>
PG_PASSWORD=<password>
PG_DATABASE=<jsdemo>


sorry about the formatting 🙂