Load a JavaScript module.
Select the module to load by its path. Note that by default, the search
context for the path
is the source file itself, not its parent like on
POSIX systems. So, if file /foo.js
is executing and you want to load
the module /bar.js
, than you have to require ../bar.js
, not ./bar.js
.
If the path starts with a file name, that is, not with any of /
, ./
or
../
, it is said to be a global module path. Such global paths are looked
for under /sys/packages
. For example, my.package.com/foo.js
would
resolve to /sys/packages/my.package.com/foo.js
.
For compatibility reasons, there is a way to require files in POSIX mode
by setting the source file’s posix int32
attribute to 1
. Then, if file
/foo.js
is executing and you want to load the module /bar.js
, you
would have to require ./bar.js
.
File extensions must be explicitly provided. If /foo.js
is executing, and
you want to load /foo.js/bar.js
, then require it by ./bar.js
, not just
./bar
.
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
, source
, 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.)
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: can not find module.
Require fails: unexpected file type.
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 source object.
Requiring a global module.