Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serviceworker and cannot use import statement outside a module

I have an react-example of a service worker, with import statements. Like:

import { clientsClaim } from 'workbox-core';

If I use this code (react) in my service-worker, I get the message:

Uncaught SyntaxError: Cannot use import statement outside a module (at service-worker.js:1:1)

Where is the mistake?

like image 244
Jens B Avatar asked Oct 18 '25 17:10

Jens B


1 Answers

You need to bundle a service worker file that uses ES module imports, as they are not supported in all service worker runtime environments at the moment.

The Workbox documentation has some more examples of this usage. Any bundler that supports ES modules should work; I tend to use esbuild nowadays.

like image 127
Jeff Posnick Avatar answered Oct 20 '25 07:10

Jeff Posnick