flexsearch icon indicating copy to clipboard operation
flexsearch copied to clipboard

How to properly use worker in the browser mode?

Open yellow1912 opened this issue 2 years ago • 6 comments

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/[email protected]/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.

yellow1912 avatar Aug 08 '21 11:08 yellow1912

Having exactly the same issue.

rathboma avatar Sep 04 '21 02:09 rathboma

This issue seems to be this line:

https://github.com/nextapps-de/flexsearch/blob/master/src/worker/index.js#L43

I'm running in Electron, and this is incorrectly identifying the process as a node process. It's not.

rathboma avatar Sep 04 '21 02:09 rathboma

@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

rathboma avatar Sep 04 '21 02:09 rathboma

See code here - https://github.com/flexdinesh/browser-or-node/blob/master/src/index.js

rathboma avatar Sep 04 '21 02:09 rathboma

Thanks for this hint. I need to solve the merge conflict before I can merge.

ts-thomas avatar Oct 18 '21 15:10 ts-thomas

Is this a solved issue?? Cause I've been having the same issue and I'm using version 0.7.21 In the meantime, is there a way to get around this?

Gabriel-Alves-Cunha avatar Nov 25 '21 22:11 Gabriel-Alves-Cunha

This fix is included in v0.7.23

ts-thomas avatar Oct 03 '22 12:10 ts-thomas