Start Temporal worker in webpack project

Hi. There.

I am trying to get the Temporal worker working in an existing project bundled with webpack. It is a NestJS project living within a pnpm monorepo but not sure whether it matters. The following codes to start the worker failed because:

  1. There is no workflows.js in that path during runtime because everything has been bundled into a single main.js

  2. The require.resolve() return a number instead of string, which caused an immediate exception during startup.

    const workflowsPath = require.resolve(‘./temporal/workflows’)
    const worker = await Worker.create({
    workflowsPath,
    taskQueue: ‘default-task-queue’,
    activities,
    })

I have searched on Google and found this article talking about a similar problem:

And at the end the article concluded that the only way working is to change to use ts-node instead of webpack.

I am wondering whether I really can’t have a Temporal worker working with webpack? Is there other workarounds for this problem?

Thanks!

1 Like

I encountered the same issue and found a workaround:
I defined workflows.js as an asset under the project.js file (I have an NX mono repo), so it’s copied alongside main.js.

          {
            "input": "apps/{projectName}/src/workflows",
            "glob": "**/*",
            "output": ""
          }

Then, instead of using require.resolve, I “hardcoded” the workflow path: ${__dirname}/example-workflow.ts.
I’m unsure if it’s the best practice for such cases, but it worked.

I wonder if there’s a better way to solve this issue.

1 Like