Using temporal with nx monorepo

Hello,
We are trying to use temporal inside an Nx monorepo with pnpm but encoutering issues that seem to be node_modules related.
We are using a webpack to package all worker related code with the worker being the entry point. When trying to start the worker, getting the following error:

Module not found: Error: Can't resolve './<omitted>/src/temporal/workflows.ts' in '/<omitted>/src/temporal' 
2022-07-06T07:46:20.137Z [ERROR] resolve './<omitted>/src/temporal/workflows.ts' in '/<omitted>/src/temporal' 
... 
Field 'browser' doesn't contain a valid alias configuration

Also tried passing in a workflowBundle which allowed the worker to start but when I try to start a new workflow, the error just happens then.

Created a minimal reproduction repo:

To see the issue simply run:

npm i 
nx serve-temporal temporal

Why use webpack on all worker code?

The worker bundles workflow code using webpack, but in all our samples, the worker and activity code aren’t bundled.

The way that nx creates the project.json configuration is using webpack to package the app code.
We placed the worker in the same project as the web app and so created a similar build configuration.
Are you saying that this is the wrong way to go about it? Do the worker and activities (and workflows?) need to live in a separate location and be bundled differently? I would love to understand more about this.

Thank you.

Instead of the webpack executor, can you use this?

You should be able to compile TS to JS and then run the worker with node. The equivalent in the hello-world sample would be:

# compiles src/*.ts to lib/*.js
npm run build
# runs worker
node lib/worker.js

The Worker and Activities are loaded into that Node process, and the code at ./workflows is bundled for you and run in vms when needed:

You can’t bundle the worker because it has binary (compiled) dependencies

Using tsc worked for me, thank you.
I am still having trouble using this in my original project which includes multiple projects with dependencies, but it does not seem to be a temporal issue anymore.

Just a follow up:
I was eventually able to run temporal on top of an nx monorepo, but it was very hard and had to use tips from here:

And also use the bare “run-commands” executor to run the worker.

1 Like