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

"Error: Cannot find module '/engine-learn/Worker'" or "Error: expose() called in the master thread." #465

Open
appocalypseltd opened this issue Feb 14, 2023 · 1 comment

Comments

@appocalypseltd
Copy link

appocalypseltd commented Feb 14, 2023

My Nx setup using threads.js ain't working...I'm getting either this error:

Error: Cannot find module 'engine-learn/Worker'

...when my setup looks like this:

// index.ts
export * from './EngineLearn';

// Worker.ts
import { expose } from 'threads/worker';

const trainModel = async () => {
  console.log("In trainModel");
};

expose(trainModel);

// EngineLearn.ts
import { Pool, spawn, Worker } from 'threads';

export class EngineLearn {
  trainModels = async () => {
    console.log('Hello');
    const pool = Pool(() => spawn(new Worker('./Worker')));
  };
}

...or, if I export Worker.ts in my root index.ts, like this:

// index.ts
export * from './EngineLearn';
export * from './Worker';

...I get:

"Error: expose() called in the master thread."

It seems the module is not included in the build if I export it in index.ts, and so cannot be found; but, if I do, expose() is immediately invokes and it gives rise to the other error.

What to do 🤷🏻

I am using the @nrwl/esbuild:esbuild build executor.

@appocalypseltd appocalypseltd changed the title "Error: Error: Cannot find module '/engine-learn/Worker'" or "Error: expose() called in the master thread." "Error: Cannot find module '/engine-learn/Worker'" or "Error: expose() called in the master thread." Feb 14, 2023
@prassie
Copy link

prassie commented Aug 19, 2023

try exposing only in a worker thread.

import { expose, isWorkerRuntime } from "threads/worker";

if (isWorkerRuntime()) {
  expose(trainModel);
}

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

2 participants