SyntaxError in firebase-messaging-sw.js: import statement outside a module
Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:1:1)Short answer
A service worker is a classic script, so an import line in firebase-messaging-sw.js is a syntax error. Firebase documents two ways out: bundle the worker so the imports are resolved at build time, or use the namespaced compat packages through importScripts, which needs no build step.
Fix it fast
1Find out which cause you have
Open the worker file as the browser receives it and look at its first line. If it starts with import, that line is the error.
curl -s https://example.com/firebase-messaging-sw.js | head -n 52Match what you see, then jump to its fix
What does the error look like?
Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:1:1)
Cause 1 of 2
The worker file uses import and is served as-is
1Check
A worker file that is served as a plain static file reaches the browser untouched. The modular imports are never resolved, so the browser rejects them.
head -n 5 public/firebase-messaging-sw.js2Fix
Use the compat form from the Firebase docs. importScripts loads the SDK from Google's CDN, and no bundler is needed.
importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-messaging-compat.js');
firebase.initializeApp({ /* your config */ });
const messaging = firebase.messaging();
messaging.onBackgroundMessage((payload) => {
self.registration.showNotification('Background Message Title', {
body: 'Background Message body.',
});
});3Verify
Unregister the old worker in the browser's Application tab, reload, and confirm the worker shows as activated with no error.
curl -s -I https://example.com/firebase-messaging-sw.jsConfirmed by firebase.google.com
Cause 2 of 2
You want the modular API inside the worker
1Check
The modular worker imports getMessaging and onBackgroundMessage from firebase/messaging/sw. That file has to go through a bundler first.
grep -rn "firebase/messaging/sw" .2Fix
Bundle the worker file with your build tool and serve the output as firebase-messaging-sw.js from the root of the domain. The Firebase guide says this bundling is required for the modular SDK in a service worker.
import { initializeApp } from 'firebase/app';
import { getMessaging, onBackgroundMessage } from 'firebase/messaging/sw';
const firebaseApp = initializeApp({ /* your config */ });
const messaging = getMessaging(firebaseApp);
onBackgroundMessage(messaging, (payload) => {
self.registration.showNotification('Background Message Title', {
body: 'Background Message body.',
});
});3Verify
Fetch the built file: its first line must no longer start with import.
curl -s https://example.com/firebase-messaging-sw.js | head -n 3Confirmed by firebase.google.com
How do I stop it coming back?
- Keep the worker small and in a single file whose output you can read, so a stray import is easy to spot.
- After each deploy, fetch the worker URL and read the first lines instead of trusting the build.
- Match the SDK version in the importScripts URLs to the firebase version in package.json.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Web apps
Attempted import error: firebase/app has no default export
Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').
Since the modular SDK (version 9), firebase/app no longer exports one firebase object.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Firebase sign-in fails with 'missing initial state' after a redirect
This may happen if browser sessionStorage is inaccessible or accidentally cleared.
The sign-in page is opened on one domain and the result is read back from another.
3 causes, each with a fixWeb apps
Google sign-in popup blocked by Cross-Origin-Opener-Policy
The page loses its reference to the sign-in popup window, because the opener policy severs it
If your site sends Cross-Origin-Opener-Policy: same-origin, a sign-in popup from another origin lands in a separate browsing context group, so your page cannot reach it.
3 causes, each with a fixWeb apps
Browser blocks a Firebase Storage request at the CORS preflight
Response to preflight request doesn't pass access control check: It does not have HTTP ok status
A browser sends an OPTIONS preflight before some cross-origin requests, and Cloud Storage answers it only from the CORS configuration on the bucket.
3 causes, each with a fixWeb apps
Firebase Messaging cannot register its default service worker
messaging/failed-service-worker-registration
Firebase Messaging registers a file called firebase-messaging-sw.js from the root of your domain.
3 causes, each with a fix