title: How to load web worker using webpack's worker loader
body: <p>I'm trying to load a web worker script into my React application by using webpack's worker loader. However, I get a module not found error.</p>
  
  <blockquote>
    <p>ERROR in ./src/index.js    Module not found: Error: Can't resolve
    './file.worker' in 'C:\Users\wyrm\Documents\Project\src'</p>
  </blockquote>
  
  <p>This is my src/index.js which is the entry point for webpack</p>
  
  <pre><code>import React from "react";
  import ReactDOM from "react-dom";
  import Worker from "./file.worker";
  
  const worker = new Worker();
  worker.postMessage({ message: "Hello there" });
  
  const Index = () =&gt; &lt;div&gt;Hello there General Kenobi!&lt;/div&gt;;
  
  ReactDOM.render(&lt;Index /&gt;, document.getElementById("index"));
  </code></pre>
  
  <p>I have my web worker script called file.worker.js in the Project/public folder</p>
  
  <pre><code>/* eslint-env worker */
  
  onmessage = e =&gt; {
    const message = e.data;
    console.log(`[From Main]: ${message}`);
  };
  </code></pre>
  
  <p>And this is my webpack config file:</p>
  
  <pre><code>const HtmlWebPackPlugin = require("html-webpack-plugin");
  
  const htmlPlugin = new HtmlWebPackPlugin({
    template: "./src/index.html",
    filename: "./index.html"
  });
  
  module.exports = {
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: [{ loader: "babel-loader" }, { loader: "eslint-loader" }]
        },
        {
          test: /\.worker\.js$/,
          use: [{ loader: "worker-loader", options: { publicPath: "/public/" } }]
        }
      ]
    },
    plugins: [htmlPlugin]
  };
  </code></pre>
  
  <p>According to the webpack <a href="https://webpack.js.org/loaders/worker-loader/#config" rel="noreferrer">documentation</a>, all I need to do is to add the following to my config file, which I have done:</p>
  
  <pre><code>// webpack.config.js
  {
    module: {
      rules: [
        {
          test: /\.worker\.js$/,
          use: { loader: 'worker-loader' }
        }
      ]
    }
  }
  </code></pre>
  
  <p>I went to the further step of specifying the public path as documented <a href="http:////%20App.js%20//%20This%20will%20cause%20the%20worker%20to%20be%20downloaded%20from%20%60/workers/file.worker.js%60%20import%20Worker%20from%20&#39;./file.worker.js&#39;;%20%20//%20webpack.config.js%20%7B%20%20%20loader:%20&#39;worker-loader&#39;%20%20%20options:%20%7B%20publicPath:%20&#39;/workers/&#39;%20%7D%20%7D" rel="noreferrer">here</a>:</p>
  
  <pre><code>// App.js
  // This will cause the worker to be downloaded from `/workers/file.worker.js`
  import Worker from './file.worker.js';
  
  // webpack.config.js
  {
    loader: 'worker-loader'
    options: { publicPath: '/workers/' }
  }
  </code></pre>
  
  <p>But even though I have specified the publicPath to be Project/public, the error thrown seems to indicate that it's looking in the Project/src folder for the web worker script. It also seems to me that webpack looks for the web worker script in the Project/public folder by default anyway, so by right it should be redundant to specify Project/public for the publicPath?</p>
  
  <p>I should also mention that I'm using webpack dev server. Could it be that I'm missing some sort of configuration for my webpack dev server, and it's not picking up my web worker script?</p>
  
  <p>I really can't figure out why I'm getting a module not found error when it seems like I've followed the documentation properly.</p>
  
  <pre><code>Project directory
  Project
  - dist
    - index.html
    - main.js
  - public
    - file.worker.js
  - src
    - index.html
    - index.js
  - .babelrc
  - .eslintrc.json
  - package.json
  - webpack.config.js
  </code></pre>