question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Web workers don't work

See original GitHub issue

Describe the bug Web workers don’t work, I get the following error:

testWorker.js:1       
Uncaught SyntaxError: Unexpected token <

When I click on the error, I see the code from the index.html for some reason:

image

structure

App.vue
workers
|_ testWorker.js

App.vue

mounted() {
  let testWorker = new Worker('workers/testWorker.js')
  testWorker.postMessage('message from App.vue')
  testWorker.addEventListener("message", (event) => {
    console.log("Received data from worker:", event)
  })
}

testWorker.js

onmessage = function (event) {
  postMessage({ result: 'Reply from worker' })
}

Environment:

  • OS and version: win10
  • vue-cli-plugin-electron-builder version : 1.4.0
  • electron version: 6.0.7

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

23reactions
alexhx5commented, Sep 25, 2019

Never mind. I figured out how to make it work. For anyone else having troubles with this, here’s what I did:

  1. Install worker-loader:
yarn add -D worker-loader
  1. Add the following in your vue.config.js:
module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.worker\.js$/,
          use: { loader: 'worker-loader' }
        }
      ]
    }
    ...
  1. In your background.js (main Electron file), inside webPreferences: {} of the window, add nodeIntegrationInWorker: true if you want to be able to use Node’s modules inside the worker. Create a worker file (e.g. workers/worker1.js) and create a listener in there, here’s some example:
const path = require('path')

// Listen to messages from parent thread
self.addEventListener('message', (event) => {
  console.log('Worker received message:', event.data)
  // Do some calculations and send the result back to parent thread
  let parsedPath = path.parse('C:/test')
  self.postMessage({result: parsedPath})
})
  1. Import and init the worker where you need it:
<script>
import Worker1 from 'worker-loader!../workers/worker1'
let worker = new Worker1()
...
mounted() {
  worker.addEventListener("message", function (event) {
    console.log('Received data from worker:', event.data)
  })
}
...
methods: {
  someMethod() {
    // Send some message to the worker
    worker.postMessage({parse: 'C:/test'})
  }
}

Notes: Don’t try to use node’s native worker_threads, they don’t work in Electron

Tags for people who’s trying to find it:

  • Web worker Module not found: Error: Can’t resolve
  • Webpack electron-builder web worker doesn’t work
  • How to use modules inside web worker in electron webpack
3reactions
nklaymancommented, Sep 25, 2019

Thanks for providing such a detailed guide! I will link to this in the docs when I get the chance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Web Worker not working [closed] - Stack Overflow
my code of javascript is not working, i expect it to start counting from 1 to infinite when the user clicks the "start...
Read more >
Web Workers not working · Issue #5180 · parcel-bundler/parcel
bug report If you create a worker and use #3654 it seems to inject react-refresh and breaks the worker: Worker1: Uncaught ReferenceError: ...
Read more >
The State Of Web Workers In 2021 - Smashing Magazine
Workers have a bad rep, but can be an important and useful tool in any web developer's toolbelt for these kinds of problems....
Read more >
Using Web Workers - Web APIs - MDN Web Docs
Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering ......
Read more >
web workers on Chrome don't work locally - Google Groups
web workers on Chrome don't work locally. 1645 views ... webserver and view the page through my localhost, a web worker process
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found