require()

Load a JavaScript library.

Syntax

require(path string) obj

Parameters

path string

Select the library to load by its path. Note that the search context for the path is not the directory which contains the file that is executing, rather the file itself. So, if file /foo.js is executed, and you want to load the library /bar.js, than you have to require ../bar.js, not bar.js.

Currently, there is no central location for finding global libraries, thus path must start with one of /, ./ or ../.

File extensions must be explicitely provided. If /foo.js is executing, and you want to load /foo.js/bar.js, then require it by ./bar.js, not just ./bar.

Description

Requiring a module allows you to build libraries (modules) and reuse them across your applications. The CommonJS 1.1 specification is implemented.

The following variables are injected in the required executing context:

module
exports
require

Variables f, context, app, request and response are not available. The callee must pass them in for use.

Modules MUST NOT define variables on the global scope. (Always use var when defining them.)

Examples

Publishing a function directly via module.exports.

Publishing a function via returning it.

Publish multiple functions.

Nested require() with absolute path.

Nested require() with relative path.

Example for using module.id.

Example for using module.file.

Require fails: no such file.

Catching a require that fails.

Catching a require that fails due to invalid JavaScript.

Require fails: invalid JavaScript.

Required code does not have access to response object.

Required code does not have access to request object.

Required code does not have access to f object.

Required code does not have access to context object.

Required code does not have access to app object.