Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this JavaScript "require"?

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:1234@localhost/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?

like image 251
neuromancer Avatar asked Mar 28 '12 04:03

neuromancer


People also ask

Can we use require in JavaScript?

“Require” is built-in with NodeJSWith require , you can include them in your JavaScript files and use their functions and variables. However, if you are using require to get local modules, first you need to export them using module. exports .

What is NodeJS require?

Node.js follows the CommonJS module system, and the builtin require function is the easiest way to include modules that exist in separate files. The basic functionality of require is that it reads a JavaScript file, executes the file, and then proceeds to return the exports object.

How do you fix require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

Should I use require or import?

Require is Non-lexical, it stays where they have put the file. Import is lexical, it gets sorted to the top of the file. It can be called at any time and place in the program. It can't be called conditionally, it always run in the beginning of the file.


2 Answers

So what is this "require?"

require() is not part of the standard JavaScript API. But in Node.js, it's a built-in function with a special purpose: to load modules.

Modules are a way to split an application into separate files instead of having all of your application in one file. This concept is also present in other languages with minor differences in syntax and behavior, like C's include, Python's import, and so on.

One big difference between Node.js modules and browser JavaScript is how one script's code is accessed from another script's code.

  • In browser JavaScript, scripts are added via the <script> element. When they execute, they all have direct access to the global scope, a "shared space" among all scripts. Any script can freely define/modify/remove/call anything on the global scope.

  • In Node.js, each module has its own scope. A module cannot directly access things defined in another module unless it chooses to expose them. To expose things from a module, they must be assigned to exports or module.exports. For a module to access another module's exports or module.exports, it must use require().

In your code, var pg = require('pg'); loads the pg module, a PostgreSQL client for Node.js. This allows your code to access functionality of the PostgreSQL client's APIs via the pg variable.

Why does it work in node but not in a webpage?

require(), module.exports and exports are APIs of a module system that is specific to Node.js. Browsers do not implement this module system.

Also, before I got it to work in node, I had to do npm install pg. What's that about?

NPM is a package repository service that hosts published JavaScript modules. npm install is a command that lets you download packages from their repository.

Where did it put it, and how does Javascript find it?

The npm cli puts all the downloaded modules in a node_modules directory where you ran npm install. Node.js has very detailed documentation on how modules find other modules which includes finding a node_modules directory.

like image 104
Joseph Avatar answered Oct 12 '22 10:10

Joseph


Alright, so let's first start with making the distinction between Javascript in a web browser, and Javascript on a server (CommonJS and Node).

Javascript is a language traditionally confined to a web browser with a limited global context defined mostly by what came to be known as the Document Object Model (DOM) level 0 (the Netscape Navigator Javascript API).

Server-side Javascript eliminates that restriction and allows Javascript to call into various pieces of native code (like the Postgres library) and open sockets.

Now require() is a special function call defined as part of the CommonJS spec. In node, it resolves libraries and modules in the Node search path, now usually defined as node_modules in the same directory (or the directory of the invoked javascript file) or the system-wide search path.

To try to answer the rest of your question, we need to use a proxy between the code running in the the browser and the database server.

Since we are discussing Node and you are already familiar with how to run a query from there, it would make sense to use Node as that proxy.

As a simple example, we're going to make a URL that returns a few facts about a Beatle, given a name, as JSON.

/* your connection code */  var express = require('express'); var app = express.createServer(); app.get('/beatles/:name', function(req, res) {     var name = req.params.name || '';     name = name.replace(/[^a-zA_Z]/, '');     if (!name.length) {         res.send({});     } else {         var query = client.query('SELECT * FROM BEATLES WHERE name =\''+name+'\' LIMIT 1');         var data = {};         query.on('row', function(row) {             data = row;             res.send(data);         });     }; }); app.listen(80, '127.0.0.1'); 
like image 31
Timothy Meade Avatar answered Oct 12 '22 08:10

Timothy Meade