How to debug a Web Worker

APR 12 2012

Recently I've been playing around with JavaScript's Web Workers. (If I create anything half decent I'll add it to my sandbox).

Pretty much immediately I hit a problem, as Web Workers do not have access to the window (or document) property of the calling document, both console and alert() are undefined. Nightmare! Apparently access to these properties is not thread safe.

The best solution in my opinion is console4Worker on GitHub.

Simply include the library as the first line in your WebWorker script

importScripts('/lib/console4Worker/console4Worker-worker.js');

And console.log(), console.error(), console.group() etc. etc. away as you normally would. Perfect!

Thanks jeromeetienne!