Using require() in an addon

I am trying to write an addon which makes some changes to the key bindings. I have included the following line:

var kb=require("ko/keybindings");

but I find it doesn’t work. It does work if I do this in a Macro.

To confirm this, I have:

console.log(0);
require("ko/keybindings");
console.log(1);

and I only get the 0.

In Komodo Edit 10, can I use the require() function? If not, what would be the alternative?

You’re likely calling it from a sub-window where require is not yet defined. This is fixed in Komodo 11 but in 10 you have to work around it by loading the require module itself. You can do this by including the following JS in your window:

let [Cc, Ci] = [Components.classes, Components.interfaces];
Cc["@mozilla.org/moz/jssubscript-loader;1"]
  .getService(Ci.mozIJSSubScriptLoader)
  .loadSubScript("chrome://komodo/content/jetpack.js", this);

Thanks for the code. However, it’s not working at all for me.

I do have a dialog window which is called from a menu item. However, when I wrap the above statement between console.log(…), I find that it fails.

I have tried this at the very beginning of the script, as well as inside the function which needs the require. I have also tried with and without 'use strict';. I have also tried to break it down into components, and it appears that Cc["@mozilla.org/moz/jssubscript-loader;1"] is not working.

Try to replace Cc with Components.classes and Ci with Components.interfaces. (or simply define Cc and Ci variables)

Brilliant. Worked Perfectly. Thanks.

I’ve updated @nathanr’s post for those who’d look for a working example.