Integrating the Javascript SDK with Common JS module

Our application uses CommonJS module and is built on top of node 14. The npm package is built using ESM. Is there a recommended way to include it as a CommonJS module?

Getting a lot of errors while trying to transpile using babel. Has anyone here done this before?

not done before, but am interested in making it work. what do you mean by EJS please? i only know EJS as javascript templates, but that doesnt make sense for npm packages

Sorry that was a typo. I meant ESM.

The import syntax cannot be mixed with require and I need the temporalio SDK to be transpiled to CommonJS.

Hi pratik, you should find that the Temporal npm packages define exports in a CJS-compatible way, by adding properties to exports:

less node_modules/@temporalio/client/lib/workflow-client.js
...
exports.WorkflowClient = WorkflowClient;

Also, in our hello-world sample, you can see this working: during the build step, our TS import statements are compiled to require() statements, for instance in lib/client.js:

const client_1 = require("@temporalio/client");

If you can share the repo or the errors, I might be able to help further :relaxed:

Thanks @loren

I was able to make it work using the CommonJS syntax i.e require.

I was working on a project that was developed on node@10.x where it would not work out of the box. Moved to another project that was using node@14.x.

You’re welcome! Yeah, the SDK only supports Node v14+. Here are a couple samples that might be relevant:

Un-transpiled JS with imports: samples-typescript/hello-world-js at main · temporalio/samples-typescript · GitHub

TS & ESM: samples-typescript/fetch-esm at main · temporalio/samples-typescript · GitHub

Thanks, @loren - Would you also have TS/JS examples for SAGA implementations with Temporal?

I can see that the Java Client supports SAGA out of the box: samples-java/TripBookingWorkflowImpl.java at main · temporalio/samples-java · GitHub

No sample yet. You could certainly code a SAGA with the current SDK, but I also opened an FR to provide helpers for it. Feel free to chime in if you have API ideas :relaxed: [Feature Request] Add SAGA helpers? · Issue #456 · temporalio/sdk-typescript · GitHub