0

I have a firebase function that uses worker_threads to off load some cpu intensive tasks. To initialize the workerThread I give it a local file path to the worker file. But when the firebase function is executed I get the following error.

An error occurred: Error: Cannot find module '/workspace/worker.js'

The worker.js file resides in the same folder as the function calling new Worker(); they both live at the root level of the src directory.

I have tried using path.resolve with no avail. I can't find anything about using web workers AND firebase functions on the internet.

How do I specify the new Workers file path to find the file locally?

Reproduce:

File Structure:

lib
   - firebase_function.js
   - worker.js
   - index.js
src
   - firebase_function.ts
   - worker.ts
   - index.ts

1) Create a firebase v2 function:

import {onCall} from "firebase-functions/v2/https";
import {Worker} from "worker_threads";

const oncallablefunction = onCall(async (request) => {
    const result = await new Promise((resolve, reject) => {
        const worker = new Worker("./worker.js"); // <- errors here, file cannot be found
        worker.on('message', resolve);
        worker.on('error', reject);
    });

    // should be 1000
    console.log(result);
})

export {oncallablefunction}

2) Create worker.js file in the same directory as the firebase function

import {parentPort} from "worker_threads";

function doWork() {
    // do computation
    return 1000
}

const result = doWork()

if (parentPort) {
    parentPort.postMessage(result);
}

3) Export from index.js

import admin from "firebase-admin";

admin.initializeApp();

export * from './firebase_function.js';

4) Deploy and execute the oncallablefunction from firebase_function.js and get the following error:

Unhandled error Error: Cannot find module '/workspace/worker.js' at Function.Module._resolveFilename

8
  • It would be helpful if you edited the question to showed the complete minimal code that reproduces the issue, including everything we need (including setup instructions) to duplicate the issue ourselves. We can't see if you might be doing something wrong. Commented May 12, 2023 at 23:32
  • @DougStevenson Reproducible steps have been added Commented May 13, 2023 at 0:03
  • Could you edit the question to share your directory structure after all this? Commented May 13, 2023 at 0:06
  • @DougStevenson Updated with the file structure if that helps. Commented May 13, 2023 at 0:30
  • Where does worker.js end up after worker.ts is compiled? Commented May 13, 2023 at 0:52

2 Answers 2

0

Your code is telling the worker to load javascript from worker.js in the current working directory of the server. This is not the same as the directory where your source code lives.

The working directory of a Cloud Functions deployment is going to be the root of your functions folder after it's unpacked. However your worker.js is going to be located at ./lib/worker.js. Your argument to Worker needs to use that path instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Great that works, Thanks! Can you explain how one would point to a file if the project was a thirdparty package? I'd like to copy worker.ts to another project. As well as copy the initializer of the worker, "new Worker('./worker.js')", into the same project as worker.ts. This new project will be install into the firebase functions project. But with that that logic, the file path would have to point to node_modules. "./node_modules/project/worker.js". Is that right? or is there another way to do that?
That sounds like a question to post separately along with as many details you have about the specific scenario.
0

Instead of specifying an absolute path which is subject to change. Use this to find the relative path:

import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

path.resolve(__dirname, 'worker.js')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.