Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker not working in production build #378

Open
fhollste opened this issue Mar 2, 2023 · 5 comments
Open

Worker not working in production build #378

fhollste opened this issue Mar 2, 2023 · 5 comments

Comments

@fhollste
Copy link

fhollste commented Mar 2, 2023

I've setup a worker with

new Document({
   ...
  worker: 1
})

Everything word fine in development but then trying to run a production build (Nextjs with webpack) I get an error
`Error: Cannot find module '/Users/fredrik/dist/node/node.js'

This is most likely due to "../dist/node/node.js" not being part of the bundle.

Any ideas or workarounds for this?

@vincesp
Copy link

vincesp commented May 17, 2023

Seems to be this line:

eval('new (require("worker_threads")["Worker"])("../dist/node/node.js")')

The first parameter for new Worker() is fileName which is a path relative to the current working directory. So above line only works if you start your script from a working directory inside of the flexsearch package.

@vincesp
Copy link

vincesp commented May 17, 2023

So here would be a workaround:

const path = require('node:path')
const { Worker } = require('flexsearch')

function createWorker() {
  const cwd = process.cwd()
  try {
    process.chdir(path.dirname(require.resolve('flexsearch')))
    return new Worker()
  } finally {
    process.chdir(cwd)
  }
}

const myWorker = createWorker()

Or with return new Document() accordingly.

@ts-thomas
Copy link
Contributor

Why you bundle a Node.js Server-side-only application?

@vincesp
Copy link

vincesp commented Mar 11, 2025

It is part of an Electron backend. The MSI installer that Electron uses only copies files, it does not do an npm install, so we have to bundle all dependencies.

@ts-thomas
Copy link
Contributor

Here are some example of the new version: https://github.com/nextapps-de/flexsearch/tree/v0.8-preview?tab=readme-ov-file#example-nodejs
The worker import was improved but maybe has still some issues. I really would like to improve that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants