Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

SyntaxError in firebase-messaging-sw.js: import statement outside a module

ErrorUncaught 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.

  • Applies to Firebase JS SDK 12 and 13, Chrome
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 5
  2. 2Match what you see, then jump to its fix

    1. 1The first line of the served file is import { ... } from 'firebase/...'The worker file uses import and is served as-isFix
    2. 2The project uses modular imports everywhere else and you want the worker to matchYou want the modular API inside the workerFix

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.js

2Fix

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.js

Confirmed by firebase.google.com

Did this fix it?

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 3

Confirmed by firebase.google.com

Did this fix it?

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:

Related fixes

Web 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 fix

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.