title: FirebaseError browserErrorMessage: "Failed to register a ServiceWorker: ServiceWorker script evaluation failed"
body: <p>I have used FCM as per this <code>https://firebase.google.com/docs/cloud-messaging/js/client</code>, and I am getting below error:</p>
  <pre><code>FirebaseError {code: &quot;messaging/failed-serviceworker-registration&quot;, message: &quot;Messaging: We are unable to register the default s…ed (messaging/failed-serviceworker-registration).&quot;, browserErrorMessage: &quot;Failed to register a ServiceWorker: ServiceWorker script evaluation failed&quot;, stack: &quot;FirebaseError: Messaging: We are unable to registe…tp://localhost:3000/static/js/0.chunk.js:57713:26&quot;}
  browserErrorMessage: &quot;Failed to register a ServiceWorker: ServiceWorker script evaluation failed&quot;
  
  code: &quot;messaging/failed-serviceworker-registration&quot;
  message: &quot;Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: ServiceWorker script evaluation failed (messaging/failed-serviceworker-registration).&quot;
  stack: &quot;FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: ServiceWorker script evaluation failed (messaging/failed-serviceworker-registration).↵    at http://localhost:3000/static/js/0.chunk.js:57713:26&quot;
  __proto__: Error
  </code></pre>
  <p>What I am doing:</p>
  <ol>
  <li><p>Created a react app by create-react-app pushnotification</p>
  </li>
  <li><p>Downloaded firebase using sudo npm install --save firebase in my project</p>
  </li>
  <li><p>created a firebase.js file in src folder of the react app with below content</p>
  <pre><code>import firebase from 'firebase';
  
  var config = {
      apiKey: &quot;MY_KEY&quot;,
      authDomain: &quot;MY_Domain&quot;,
      databaseURL: &quot;MY_DBurl&quot;,
      projectId: &quot;My_ProjectID&quot;,
      storageBucket: &quot;My_StorageBucket&quot;,
      messagingSenderId: &quot;My_Msg_id&quot;
  };
  firebase.initializeApp(config);
  
  const messaging = firebase.messaging();
  
  messaging.requestPermission().then(function(){
      console.log(&quot;Have Permission&quot;);
  }).catch(function(error){
      console.log(error);
  })
  </code></pre>
  </li>
  </ol>
  <p>and when I refresh my web app it asks for the permission like below:</p>
  <p><a href="https://i.stack.imgur.com/REcxq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/REcxq.png" alt="enter image description here" /></a></p>
  <p>So far everything is working, now I am updating my firebase.js file like below to send push notification:</p>
  <pre class="lang-js prettyprint-override"><code>import firebase from 'firebase';
  
  var config = {
      apiKey: &quot;MY_KEY&quot;,
      authDomain: &quot;MY_Domain&quot;,
      databaseURL: &quot;MY_DBurl&quot;,
      projectId: &quot;My_ProjectID&quot;,
      storageBucket: &quot;My_StorageBucket&quot;,
      messagingSenderId: &quot;My_Msg_id&quot;
  };
  firebase.initializeApp(config);
  
  const messaging = firebase.messaging();
  
  messaging.requestPermission().then(function(){
      console.log(&quot;Have Permission&quot;);
      return messaging.getToken();
  }).then(function(token){
      console.log(token)
  }).catch(function(error){
      console.log(error);
  });
  
  messaging.onMessage(function(payload){
      console.log(payload);
  })
  </code></pre>
  <p>also I have create a firebase-messaging-sw.js file with below content:</p>
  <pre class="lang-js prettyprint-override"><code>import firebase from 'firebase';
  
  var config = {
      apiKey: &quot;MY_KEY&quot;,
      authDomain: &quot;MY_Domain&quot;,
      databaseURL: &quot;MY_DBurl&quot;,
      projectId: &quot;My_ProjectID&quot;,
      storageBucket: &quot;My_StorageBucket&quot;,
      messagingSenderId: &quot;My_Msg_id&quot;
  };
  firebase.initializeApp(config);
  
  const messaging = firebase.messaging();
  </code></pre>
  <p>In my public folder and now when I do a page refresh I get above error:</p>
  <p><a href="https://i.stack.imgur.com/KC095.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KC095.png" alt="enter image description here" /></a></p>