What is this Javascript “require”?

What is this Javascript “require”?

I'm trying to get Javascript to read/write to a PostgreSQL database. I found this&nbsp;<a href="https://github.com/brianc/node-postgres" target="_blank">project</a>&nbsp;on github. I was able to get the following sample code to run in node.

I'm trying to get Javascript to read/write to a PostgreSQL database. I found this project on github. I was able to get the following sample code to run in node.

var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`
var conString = "tcp://postgres:[email protected]/postgres";

var client = new pg.Client(conString); client.connect();

//queries are queued and executed one after another once the connection becomes available client.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)"); client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['Ringo', 67, new Date(1945, 11, 2)]); client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]);

//queries can be executed either via text/parameter values passed as individual arguments //or by passing an options object containing text, (optional) parameter values, and (optional) query name client.query({ name: 'insert beatle', text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", values: ['George', 70, new Date(1946, 02, 14)] });

//subsequent queries with the same name will be executed without re-parsing the query plan by postgres client.query({ name: 'insert beatle', values: ['Paul', 63, new Date(1945, 04, 03)] }); var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);

//can stream row results back 1 at a time query.on('row', function(row) { console.log(row); console.log("Beatle name: %s", row.name); //Beatle name: John console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates console.log("Beatle height: %d' %d"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints });

//fired after last row is emitted query.on('end', function() { client.end(); });

Next I tried to make it run on a webpage, but nothing seemed to happen. I checked on the Javascript console and it just says "require not defined."

So what is this "require?" Why does it work in node but not in a webpage?

Also, before I got it to work in node, I had to do npm install pg. What's that about? I looked in the directory and didn't find a file pg. Where did it put it, and how does Javascript find it?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Secure Node.js, Express.js and PostgreSQL API using Passport.js

The comprehensive step by step tutorial on building secure Node.js, Express.js, Passport.js, and PostgreSQL Restful Web Service

How to Use Express.js, Node.js and MongoDB.js

In this post, I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).