'Typescript based React: importScripts() gives: 'importScripts' is not defined no-undef

Following Google Workbox instructions, the first thing I am told I should include in serviceWorker.js is:

importScripts('https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js');

But trying to run npm run start gives me:

importScripts' is not defined  no-undef

Where exactly does importScripts get defined usually, and how would I emnable using it in a Typescript-based React project made (and not ejected) with Create React App 2?



Solution 1:[1]

importScripts is defined for worker scripts, including web workers (both dedicated [new Worker] and shared [new SharedWorker]) and service workers. If it's not defined in your case, it sounds like the script being run isn't being run in one of those ways.

Solution 2:[2]

Add WebWorker within lib property of compilerOptions in tsconfig.json file. Example

{
  "compilerOptions": {
    ... 
    "lib": [
      ...,
      "WebWorker"
    ]
  }
}

P.S. After making changes, restart your IDE. Although this is not required but in most cases, it works :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 T.J. Crowder
Solution 2 Aakash Goplani