2

With jpm, one now (from Firefox 38 on) has the possibility to use npm modules in Firefox Add-ons, cf. the documentation on MDN. This appears to be restricted to the main entry file (index.js by default), especially since require() isn't defined in content scripts.

Is there any way of using npm modules in content scripts for Firefox-Addons?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249

1 Answers1

1

You can get the url of a file via require.resolve, so use something like:

contentScriptFile: [ require.resolve("something/lib/file.js"), require("sdk/self").data.url("content-script.js") ]

This will use a file from npm in a content script.

If the npm file is a CommonJS file then this becomes more tricky, you might have to read the content of the file, and pass that to the content script (using message passing or the contentScriptOptions) to have it evaluated with requireJS or something.

erikvold
  • 15,988
  • 11
  • 54
  • 98