Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'require(...)' a common javascript pattern or a library function?

I usually find this as the first line in node.js scripts/modules as well as phantomJS, casperJS etc. I'm curious, if this is a common pattern for server-side javascript (SSJS) (similar to #include in C/C++ or import in Java) or is it a a library like RequireJS or LabJS that is being called for this inclusion (neither of which I have gotten a chance to use in practice, as yet)?

e.g. var http = require('http') or var casper = require('casper').create()

I'm curious if this is a pattern that has become standardized for SSJS or does every library/tool call an existing function?

Please pardon the n00b dimension to the question but I would like to know the 'why' behind its omnipresence :)

like image 360
PhD Avatar asked Jul 18 '12 23:07

PhD


People also ask

What is require () in JavaScript?

require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.

What is require () in node JS?

2011-08-26. 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.

Can I use require in JavaScript?

“Require” is built-in with NodeJS With 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.

What is the difference between require and import in JavaScript?

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

The require() idiom is part of a specification known as CommonJS. Specifically, that part of the spec is called 'Modules'. RequireJS is just one implementation of CommonJS (and it's usually a browser-side implementation - in fact, it takes a different approach because of the asynchronous nature of the browser).

If you look at the list of implementations on the CommonJS site, you'll see Node.js listed. Notice that it implements 'Modules'. Hence, that's where it's coming from - it's very much built-in.

like image 132
voithos Avatar answered Sep 28 '22 20:09

voithos


The require in PhantomJS and Node.js means exactly the same with the difference that none of the base modules match. Although the fs module exists for both, they are different and do not provide the same functions.

require is functionally the same in PhantomJS and Node.js. CasperJS is built on top of PhantomJS and uses its require function, but also patches it. With CasperJS it is also possible to require a module with its name such as require('module') instead of require('./module') if it is in the same directory.

Full matrix (file.js is in the same directory as the executed script):

            | node
            |   | phantom
            |   |   | casper
            |   |   |   | slimer
------------+---+---+---+--------
file        | n | n | y | y
./file      | y | y | y | y
file.js     | n | n | n | n
./file.js   | n | n | n | n

PhantomJS can also use modules defined in the special folder node_modules in the same way that node does. It cannot use actual node modules that have dependencies to modules that are not present in PhantomJS.

Examples of what can be required:

m.js (for functions)

module.exports = function(){
    return {
        someKey: [1,2,3,4],
        anotherKey: function(){
            console.log("module exports works");
        }
    }
};

e.js (for everything else as JS)

exports.someKey = {
    innerKey: [1,2,3,4]
};

exports.anotherKey = function(){
    console.log("exports works");
};

a.json (arbitrary JSON)

[
    {
        "someKey": [ 1,2,3,4 ],
        "anotherKey": 3
    }
]

script.js

var m = require("./m")();
m.anotherKey(); // prints "module exports works"

var e = require("./e");
e.anotherKey(); // prints "exports works"

var a = require("./a");
console.log(a[0].anotherKey); // prints "3"
like image 31
Artjom B. Avatar answered Sep 28 '22 19:09

Artjom B.