- an object called
exportswhich contains all the objects which a CommonJS module wants to make available to other modules
- a function called
requirewhich a module can use to import the
exportsobject of another module.
- External modules: reusable modules developed and maintained outside the SDK, but usable by SDK-based add-ons.
The modules supplied by the SDK are divided into two sorts:
- High-level modules like
page-modprovide relatively simple, stable APIs for the most common add-on development tasks.
- Low-level modules like
namespaceprovide more powerful functionality, and are typically less stable and more complex.
To use SDK modules, you can pass
require() a complete path, starting with "sdk", to the module you want to use. For high-level modules this is just
sdk/<module_name>, and for low-level modules it is
// load the high-level "tabs" module var tabs = require("sdk/tabs"); // load the low-level "uuid" module var uuid = require('sdk/util/uuid');
The path to specify for a low-level module is given along with the module name itself in the title of the module's documentation page (for example, system/environment).
Although the SDK repository in GitHub includes copies of these modules, they are built into Firefox and by default, when you run or build an add-on using
cfx run or
cfx xpi, it is the versions of the modules in Firefox that are used. If you need to use a different version of the modules, you can do this by checking out the version of the SDK that you need and passing the
--overload-modules option to
cfx run or
At a minimum, an SDK-based add-on consists of a single module named
main.js, but you can factor your add-on's code into a collection of separate CommonJS modules. Each module is a separate file stored under your add-on's "lib" directory, and exports the objects you want to make available to other modules in your add-on. See the tutorial on creating reusable modules for more details.
To import a local module, specify a path relative to the importing module.
For example, the following add-on contains an additional module directly under "lib", and other modules under subdirectories of "lib":
To import modules into
// main.js code var dialog = require("./password-dialog"); var hash = require("./secrets/hash");
To import modules into
// password-store.js code var dialog = require("../password-dialog"); var hash = require("../secrets/hash");
For backwards compatibility, you may also omit the leading "
./" or "
../" characters, treating the path as an absolute path from your add-on's "lib" directory:
var dialog = require("password-dialog"); var hash = require("secrets/hash");
This form is not recommended for new code, because the behavior of
require is more complex and thus less predictable than if you specify the target module explicitly using a relative path.
As well as using the SDK's modules and writing your own, you can use modules that have been developed outside the SDK and made available to other add-on authors.
There's a list of these "community-developed modules" in the SDK's GitHub Wiki, and to learn how to use them, see the tutorial on using external modules to add menu items to Firefox.
To import external modules, treat them like local modules: copy them somewhere under your add-ons "lib" directory and reference them with a path relative to the importing module.
For example, this add-on places external modules in a "dependencies" directory:
It can then load them in the same way it would load a local module. For example, to load from
// main.js code var geo = require("./dependencies/geolocation");
The SDK freezes the
exports object returned by
require. So a if you import a module using
require, you can't change the properties of the object returned:
self = require("sdk/self"); // Attempting to define a new property // will fail, or throw an exception in strict mode self.foo = 1; // Attempting to modify an existing property // will fail, or throw an exception in strict mode self.data = "foo";