nextapps-de / flexsearch

Next-Generation full text search library for Browser and Node.js
Apache License 2.0
12.54k stars 492 forks source link

How to properly use worker in the browser mode? #251

Closed yellow1912 closed 2 years ago

yellow1912 commented 3 years ago

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.

rathboma commented 3 years ago

Having exactly the same issue.

rathboma commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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

ts-thomas commented 3 years ago

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

Gabriel-Alves-Cunha commented 3 years ago

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?

ts-thomas commented 2 years ago

This fix is included in v0.7.23