detect-webworker

Detect web worker environment

Usage no npm install needed!

<script type="module">
  import detectWebworker from 'https://cdn.skypack.dev/detect-webworker';
</script>

README

Install

npm install --save detect-webworker

Usage:

var isWebWorker = require('detect-webworker');

if (isWebWorker) {
  console.log("Running under Web Worker");
} else {
  console.log("Hello from not a Web Worker env");
}

The check is performed as:

module.exports = false;
try {
  module.exports = self instanceof WorkerGlobalScope
} catch(e) {}

Inspired from detect-node and idea taken from this stackoverflow issue