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.

How to properly use worker in the browser mode?

See original GitHub issue

Hello,

I’m trying to use this module in the browser. I’m using the following code according to the doc:

const { Worker } = require("flexsearch");

var myWorker = new Worker();

When I try the search, myWorker.search(‘some test’); I get the following error:

“TypeError: Cannot read property ‘postMessage’ of undefined at eval (webpack://@somepath./node_modules/.pnpm/flexsearch@0.7.21/node_modules/flexsearch/dist/flexsearch.bundle.js?:23:229)”

I’m using Webpack to build my package, it seems like I missed something obvious but I cannot figure out what.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rathbomacommented, Sep 4, 2021

@ts-thomas Could this line use the following check instead?

if (typeof window === 'undefined')
// this is node

In an electron window with nodeIntegration enabled it will look like a node process, but actually needs to be a browser process

0reactions
ts-thomascommented, Oct 3, 2022

This fix is included in v0.7.23

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Effectively Use Browser Caching and Service Workers
Learn more advanced front-end and full-stack development at: https://www.fullstackacademy.comService Workers are JavaScript web workers used ...
Read more >
Using Service Workers - Web APIs | MDN
In Firefox, Service Worker APIs are hidden and cannot be used when the user is in private browsing mode, or when history is...
Read more >
HTML Web Workers API - W3Schools
A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page.
Read more >
Web Workers Demystified - Medium
A web worker is a simple way to run Javascript code in background threads on the browser. The code in the workers will...
Read more >
Web workers in JavaScript and when to use them - bene : studio
If you put this code into the browser console in Developer Tools, you will see that the page becomes unresponsive which is due...
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