# Using ES6 modules in background scripts

**URL:** <https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911>\
**Category:** Development\
**Created:** [July 6, 2018, 5:49pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911 "2018-07-06T17:49:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rugkx](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/rugkx/32/19322_2.png) [@rugkx](https://discourse.mozilla.org/u/rugkx)\
**Post date:** [July 6, 2018, 5:49pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/1 "2018-07-06T17:49:03Z")

</div>

I looked how one may use a background script es a new ES6 module. However, it seems to be very hard to easily do this.

Usually you embed it like this.

```json
“background”: {
 “script”: [“background.js”]
}

```

However, with a ES6 module, this does not seem to work, because usually (e.g. when you embed it in a HTML you have to ad the attribute `type="module"`. In a `manifest.json` this does not seem possible…

* * *

So the problem has been discussed [in this article](https://medium.com/front-end-hacking/es6-modules-in-chrome-extensions-an-introduction-313b3fce955b) and there a simple workaround has been suggested:  
Just include a HTML file instead of (only) a JS file in the `manifest.json`:

```json
“background”: {
 “page”: “background.html”
}

```

However, this is of course not very nice and still a workaround.

- If nobody else comes up with some idea, I guess it is not implemented in Firefox. I’d open a Bugzilla issue then.
- Also, as for current actions, it should be documented [on MDN](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/background), that this is not really possible. (and maybe document the workaround)

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [July 6, 2018, 6:44pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/2 "2018-07-06T18:44:01Z")

</div>

There is at the very least an issue open to allow loading es modules as content scripts, since that is not possible with current manifest declarations. And would for sure have higher priority than adding the API for the background page. At the same time I assume adding it for the background page will be much easier once it’s possible for content scripts.

> **[1451545 - Support loading content scripts as ES6 modules](https://bugzilla.mozilla.org/show_bug.cgi?id=1451545)**
>
> NEW (nobody) in WebExtensions - General. Last updated 2018-06-19.

Currently mdn does not document at all how es modules behave, though the knowledgeable programmer would be aware that es modules require a special loading mode and thus hopefully realize that they need to load their scripts with type=“module” (which by no means is a work around).

Edit: just stumbled across the bug requesting the very thing for the bg page: [https://bugzilla.mozilla.org/show\_bug.cgi?id=1394303](https://bugzilla.mozilla.org/show_bug.cgi?id=1394303)

---

<div class="post-metadata">

**Author:** ![rugkx](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/rugkx/32/19322_2.png) [@rugkx](https://discourse.mozilla.org/u/rugkx)\
**Post date:** [July 6, 2018, 7:30pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/3 "2018-07-06T19:30:56Z")

</div>

> [@freaktechnik](#):
>
> higher priority than adding the API for the background page

Hu?`Actually background pages do work. At least syntactically Firefox accepts them (JS itself still needs some testing) and the (already linked) [MDN doc](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/background) does not mention any such limitation.

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [July 6, 2018, 7:47pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/4 "2018-07-06T19:47:53Z")

</div>

You are right, it would be mainly a matter of adding a key that triggers a different bg page template (though the properties would have to be mutually exclusive, which is fun) and it involves some C++ nonetheless.

Yet, the fact that you can already use modules with the bg page should, in my eyes, make work to add a shortcut less important than supporting them for content scripts.

---

<div class="post-metadata">

**Author:** ![rugkx](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/rugkx/32/19322_2.png) [@rugkx](https://discourse.mozilla.org/u/rugkx)\
**Post date:** [July 7, 2018, 10:09am UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/5 "2018-07-07T10:09:59Z")

</div>

Okay, so good to hear the workaround works. And sure, it is then less a priority, but would still be good. Because that background page is very obviously a **workaround** and thus no proper solution for this use case.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [April 1, 2019, 8:41am UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/6 "2019-04-01T08:41:01Z")

</div>

Hi @rugkx, could you please share your experience with ES6 modules regarding webextensions development?  
I’ve noticed even Firefox ESR 60 now [supports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Browser_compatibility) modules so there should be no reason not to use this feature, right?

I want to start big refactoring of my code so I want to be sure I won’t regret it 🙂.

---

<div class="post-metadata">

**Author:** ![rugkx](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/rugkx/32/19322_2.png) [@rugkx](https://discourse.mozilla.org/u/rugkx)\
**Post date:** [April 1, 2019, 2:46pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/7 "2019-04-01T14:46:50Z")

</div>

So, yes, I have been using ES6 modules for some time in my add-ons now.  
Except of some workarounds as mentioned in this thread here, they work nicely.

Basically you can see it being used in [https://github.com/rugk/offline-qr-code/](https://github.com/rugk/offline-qr-code/) e.g.  
I have even split many modules out of this, so I can re-use them between add-ons. See [the whole TinyWebEx org](https://github.com/TinyWebEx/). I do currently use git submodules to integrate them into the main repo(s) of the add-ons.

The only issue is that they can only statically include other files, i.e. I e.g. often include a settings file from `../data/settings.js` in one TinyWebEx module, which you have to create manually in the right place.  
But well… this is a trade-off, and certainly better than a single JS file, e.g.

I have not yet tried whether these could be published as NodeJS modules or so (WebCompat AFAIK does support ES6 modules, so they could possibly be compiled), so they can be re-used in other ways.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [May 10, 2019, 1:34pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/9 "2019-05-10T13:34:21Z")

</div>

I’ve started refactoring my code using modules and it’s really really nice! I should have done this long time ago.

I had a same problem with my “utils” modules from git submodule being wrongly referenced, but luckily you can move your submodule to a different folder using `git mv`.

**The only problem I’m having is with Web Console** - somehow I’m no longer able to call functions or access variables defined in my main module. I can still access global stuff, like `await browser.storage.local.get()`, but not my own functions or imported modules.  
**Any workarounds?**

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [May 10, 2019, 1:39pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/10 "2019-05-10T13:39:47Z")

</div>

You could probably use the dynamic `import()` function in the console to load the module that you want to call a function of.

---

<div class="post-metadata">

**Author:** ![rugkx](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/rugkx/32/19322_2.png) [@rugkx](https://discourse.mozilla.org/u/rugkx)\
**Post date:** [May 10, 2019, 2:24pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/11 "2019-05-10T14:24:38Z")

</div>

The problem is just that dynamic imports are [not yet available in Firefox](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#Browser_compatibility). (still behind an about:config setting in Firefox 66)

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [May 10, 2019, 2:28pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/12 "2019-05-10T14:28:20Z")

</div>

Yes, but Firefox Developer Edition is already 67 where it works 🙂.

As a workaround for developer it’s ok… Thank you @freaktechnik!

EDIT:

> This flag is enabled by default on nightly builds from Nightly 67.

So it will be also part of the next ESR 68 (YES!).

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [May 10, 2019, 2:49pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/13 "2019-05-10T14:49:29Z")

</div>

> [@juraj.masiar](#):
>
> > This flag is enabled by default on nightly builds from Nightly 67.
> 
> So it will be also part of the next ESR 68 (YES!).

Not necessarily, the flag will probably be kept in nightly, but it may graduate in 68.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [May 10, 2019, 3:02pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/14 "2019-05-10T15:02:59Z")

</div>

True, but it’s already set in Beta (Developer Edition), so I really hope that means it’s coming to release 67 in less than two weeks! 🙂

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [May 10, 2019, 3:04pm UTC](https://discourse.mozilla.org/t/using-es6-modules-in-background-scripts/29911/15 "2019-05-10T15:04:02Z")

</div>

Dev Edition has additional flags flipped for devs, as compared to beta.
