troubleshooting Question

Cannot read property 'addEventListener' of undefined --- navigator.serviceWorker.register('worker.js')

Avatar of Wayne Barron
Wayne BarronFlag for United States of America asked on
3 Comments1 Solution6 ViewsLast Modified:
I am trying to get a javascript to work, however. It is giving me some issues.
The demo of the working script from the author is here.
I am using the same code he has on his demo page.
I downloaded the worker.js and have it stored in my JS folder.
So it looks like this in the code.
if ('serviceWorker' in navigator) {  var iceworker = navigator.serviceWorker.register('worker.js')
//I get this error in Google Chrome
Uncaught TypeError: Cannot read property 'addEventListener' of undefined
//For this line.
navigator.serviceWorker.addEventListener('message', event => {

Here is the script from the link above.

<audio preload="none" id="aud" controls src="" type="audio/mpeg">
if ('serviceWorker' in navigator) {
  var iceworker = navigator.serviceWorker.register('metadataworker.js')
    .then(function(reg) {
      console.log('Icecast service worker registered');
      addItem('Icecast service worker registered. Click play to start the stream.');
    }).catch(function(error) {
    console.warn('Error ' + error);
    addItem('Error ' + error);
var delay = 8000;
navigator.serviceWorker.addEventListener('message', event => {
  if(event.origin != ''){
    document.querySelector('div').innerText ="'") + 1,"'"));
  addItem('Message from service worker ' +;
document.querySelector('audio').addEventListener('play', event => {
  addItem("Measuring buffering time...");
  document.querySelector('audio').addEventListener('playing', event => {
  var measures = performance.getEntriesByName("delay");
  delay = measures[0].duration;
  addItem("Buffering took " + (delay/1000).toFixed(2) + " seconds");
  addItem("Setting metadata update delay to " + (delay/1000).toFixed(2) + " seconds");
  document.querySelector('audio').addEventListener('pause', event => {
  document.querySelector('audio').src = document.querySelector('audio').src;
  function addItem(text) {
  var node = document.createElement("li");
  var textnode = document.createTextNode(text);

I've gone through his code and replaced every section with his domain, to point to mine.
This is the source link for the javascript function.
Source Code for Javascript
Can someone please assist me in getting this working?
Thank you.
Wayne Barron
Author, Web Developer
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros