Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ff-addon-template: Template for how to create a JSM module.
const {utils: Cu} = Components;
const chromeModulesPath = 'chrome://bootstrap-jsm/content/';
//Cu.import(chromeModulesPath + '/helloWorld.jsm'); //we CANNOT import it here we must do it in startup otherwise we get this error: "WARN addons.xpi: Error loading bootstrap.js for Bootstrap-JSM@jetpack: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXPCComponents_Utils.import]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: resource://gre/modules/XPIProvider.jsm -> jar:file:///C:/Documents%20and%20Settings/blah%20blah/Application%20Data/Mozilla/Firefox/Profiles/de83bb0q.default/extensions/Bootstrap-JSM@jetpack.xpi!/bootstrap.js :: <TOP_LEVEL> :: line 3" data: no]"
function startup(aData, aReason) {
Cu.import(chromeModulesPath + '/helloWorld.jsm'); //startup is the earliest we can import it
var rWin = Services.wm.getMostRecentWindow(null);
rWin.alert('foo = ' + foo);
rWin.alert('bar = ' + bar);
rWin.alert('bar.size = ' + bar.size);
rWin.alert('bar.theDummy = ' + bar.theDummy);
rWin.alert('bar.theLastVar = ' + bar.theLastVar);
//rWin.alert('dummy = ' + dummy); //dummy was not exported so it will throw this error: "WARN addons.xpi: Exception running bootstrap method startup on Bootstrap-JSM@jetpack: ReferenceError: dummy is not defined (resource://gre/modules/XPIProvider.jsm -> jar:file:///C:/Documents%20and%20Settings/blah%20blah/Application%20Data/Mozilla/Firefox/Profiles/de83bb0q.default/extensions/Bootstrap-JSM@jetpack.xpi!/bootstrap.js:16)"
function shutdown(aData, aReason) {
if (aReason == APP_SHUTDOWN) return;
Cu.unload(chromeModulesPath + '/helloWorld.jsm');
//we get rid of it here, of course on shutdown of our
//addon it will be unloaded, however
//if helloWorld.jsm was imported in another privelaged scope it will
//persist there, until restart of browser.
//after running unload however, all instances of helloWorld.jsm can now be deleted? (im not sure about this last line)
function install() {}
function uninstall() {}
/* //do this if want to make it a resource uri instead of a chrome (chrome://bootstrap-jsm/content/hellowWorld.jsm)
// Import Services.jsm unless in a scope where it's already been imported
var resProt ='resource')
var aliasFile = Components.classes[';1']
var aliasURI =;
resProt.setSubstitution('myalias', aliasURI);
// assuming the code modules are in the alias folder itself
content bootstrap-jsm ./
var EXPORTED_SYMBOLS = ['foo', 'bar'];
function foo() {
return 'foo';
var dummy = 'dummy is a string';
var bar = {
name: 'bar',
size: 3,
theDummy: dummy,
theLastVar: lastVar //when you import this jsm and try to get bar.theLastVar you will find that it is 'undefined', this is because 'lastVar' was not defined at the time this object was created
var lastVar = 'this is the last var its a string';
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at -->
<RDF xmlns="" xmlns:em="">
<Description about="urn:mozilla:install-manifest">
<!-- Firefox -->
<!-- Front End MetaData -->
<em:name>Bootstrap JSM</em:name>
<em:description>Template for how to create a JSM module.</em:description>
<em:contributor>Pat for Icon</em:contributor>

This comment has been minimized.

Copy link
Owner Author

@Noitidart Noitidart commented Feb 17, 2014


Why chrome and why not resource?

At least as of Firefox 27.0.1, we cannot use resources in bootstrap.js, if we had put in our chrome.manifest file this code:

resource bootstrap-jsm ./

It would give us this error:

Bootstrapped manifest not allowed to use 'resource' directive.

Important things to learn in each commit

Revision 1

It's bugged don't look at it.

Revision 2

Fixed. The problem was the path in the manifest file was wrong. So download XPI and install it then import helloWorld.jsm like this:


To demonstrate that theLastVar is undefined in the bar object because var lastVar was defined after that object do the following:

  • Open Scratchpad
  • Go to Environment menu and pick Browser
  • Import the module and alert bar.theLastVar, see code below
  • You will see that it alerts undefined

Revision 3

I put the example into the bootstrap file. It was important because it shows you cannot import the JSM module until startup() or after startup.

You can still do the experiment mentioned in Revision 2 section as JSM files can be imported anywhere.

Revision 4

  • Moved the Services.jsm import to outside of startup as there is no reason for it to be inside

This comment has been minimized.

Copy link

@gregglind gregglind commented Nov 2, 2017

This is a great example! I will be referencing it a lot in the Shield Study (Legacy Addon) documentation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment