title: ReferenceError: document is not defined. Service worker. Workbox
body: <p>I'm learning how to write code of service worker and stuck with the error &quot;ReferenceError: document is not defined&quot; in my app.js file. I'm using workbox library with InjectManifest mode. I think the problem in the webpack.config.js, because when I delete InjectManifest in webpack.config.js  the error disappears.</p>
  <p><strong>My webpack.config.js</strong></p>
  <pre><code>const path = require('path');
  const webpack = require('webpack');
  const HtmlWebPackPlugin = require('html-webpack-plugin');
  const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
  const {InjectManifest} = require('workbox-webpack-plugin');
  
  module.exports = {
    entry: './src/index.js',
    output: {
      path: path.resolve(__dirname, 'dist'),
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
          },
        },
        {
          test: /\.html$/i,
          use: [
            {
              loader: 'html-loader',
            },
          ],
        },
        {
          test: /\.css$/,
          use: [
            MiniCssExtractPlugin.loader, 'css-loader',
          ],
        },
        {
          test: /\.(png|jpg|gif)$/i,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 8192,
              },
            },
          ],
        },
      ],
    },
    optimization: {
      minimize: true,
      minimizer: [
        new CssMinimizerPlugin(),
      ],
    },
    plugins: [
      new HtmlWebPackPlugin({
        template: './src/index.html',
        filename: './index.html',
      }),
      new MiniCssExtractPlugin({
        filename: '[name].css',
        chunkFilename: '[id].css',
      }),
       new InjectManifest({
         swSrc: './src/js/service.worker.js',
         swDest: 'service.worker.js',
      }),
    ],
  };
  </code></pre>
  <p><strong>My service.worker.js file:</strong></p>
  <pre><code>import { precacheAndRoute } from 'workbox-precaching/precacheAndRoute';
  import { cinemaNews } from './cinemaNews';
  import { url } from './app';
  
  precacheAndRoute(self.__WB_MANIFEST);
  
  const CACHE_NAME = 'v1';
  
  const responseCache = new Response(JSON.stringify(cinemaNews));
  
  self.addEventListener('install', (evt) =&gt; {
    console.log('install')
    evt.waitUntil((async () =&gt; {
      console.log('install waitUntil')
      const cache = await caches.open(CACHE_NAME);
      await cache.put(url, responseCache);
      await self.skipWaiting();
    })());
  });
    
  self.addEventListener('activate', (evt) =&gt; {
    console.log('activate')
    evt.waitUntil(self.clients.claim());
  });
  
  self.addEventListener('fetch', (evt) =&gt; {
    console.log('sw fetch')
    const requestUrl = new URL(evt.request.url);
    
    if (!requestUrl.pathname.startsWith('/news')) return;
    
    evt.respondWith((async () =&gt; {
      console.log('respondWith')
      const cache = await caches.open(CACHE_NAME);
      const cachedResponse = await cache.match(evt.request);
      return cachedResponse;
    })());
    
    evt.waitUntil((async () =&gt; {
      console.log('waitUntil');
      const response = await fetch(evt.request.url);
      const client = await clients.get(evt.clientId);
      let json = await response.json();
      client.postMessage(json);
    })());
  });
  </code></pre>