1

Trying to import a file to keep clean background.js using manifest_version v3.

Is't allowed to import, nor am i missing something here ?

//manifest.json
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
//background.js

// error here, cant import a file at all, tried both
import {fetchUsers} from "./xxx/users.js"
const {fetchUsers} = await import(chrome.runtime.getURL('./xxx/users.js'));


chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if(request.action == 'fetchUsers'){
    let data = fetchUsers()
    sendResponse({users: data});
  }
});

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
  • 2
    We can't use ES modules in service workers yet. Use [importScripts](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts) until then. – wOxxOm Apr 04 '21 at 09:14
  • @wOxxOm good call, want to submit as an answer? I accept it. – 7urkm3n Apr 04 '21 at 09:21
  • I'd say [this answer](https://stackoverflow.com/a/66408379) already encompasses the topic. – wOxxOm Apr 04 '21 at 17:25
  • @wOxxOm Works perfectly, thanks! Please submit as answer :) – Chris Apr 04 '21 at 17:44

0 Answers0