JavaScript Compilation Issue for Embedding JS for LangChain

Langchain, a popular AI based engine, simplifies the integration of LLMs with external data sources, APIs, memory, and workflows, enabling developers to create context-aware, dynamic, and intelligent AI applications.

We are trying to embed the LangChain JavaScript Libraries for the use of BPM workflows. But we are getting an issue during compilation of code in the Text File Rule of type JS.

@Joyce Jenifer please check the Pega GenAI™ Frequently Asked Questions and documentation for limitations.

I would suggest you log your issue as an incident via the MSP.


:warning: This is a GenAI-powered tool. All generated answers require validation against the provided references.

Common Causes of JavaScript Compilation Issues in Pega Text File Rules

The issue you’re experiencing is likely related to one of the following:

  1. Module System Incompatibility: LangChain uses ES Modules, while Pega’s Text File Rule execution environment might be expecting CommonJS format. This often results in “Cannot use import statement outside a module” errors.
  2. Import Syntax Errors: The modern ES import syntax (import { something } from '@langchain/core') may not be supported in the Pega JavaScript environment.
  3. Missing Dependencies: LangChain requires several npm packages that need to be available in the execution context.

Recommended Solutions### Solution 1: Use CommonJS Import Syntax

Instead of ES Module imports, try using CommonJS require statements:

// Instead of:
// import { ChatOpenAI } from "@langchain/openai";

 
// Use:
const LangChain = require('@langchain/core');
const OpenAI = require('@langchain/openai');

Solution 2: Bundle Libraries Before Importing

Pre-bundle LangChain and its dependencies using a tool like Webpack or Rollup, then include the bundled file in your Pega application:

  1. Create a bundle file outside Pega using:
  2. npm install @langchain/core @langchain/openai
  3. npx webpack --config webpack.config.js
  4. Add the resulting bundle as a Text File Rule, and reference it in your other JavaScript files.

Solution 3: Use IIFE Format Libraries

Look for IIFE (Immediately Invoked Function Expression) versions of LangChain libraries which can be directly included without import statements.

Solution 4: Check JavaScript Version Compatibility

Ensure your Text File Rule settings are configured to support modern JavaScript:

  1. Navigate to Dev Studio > Create > Technical > Text File
  2. Configure the text file with:
    • Appropriate Label
    • App Name (Directory): webwb
    • File Type (extension): js

Then add your JavaScript code to the main tab, ensuring it uses syntax compatible with the JavaScript environment Pega supports.

Additional Considerations

  • LangChain may require external API access. Ensure your Pega environment has the necessary network permissions to reach these services.
  • Consider creating a custom JavaScript wrapper that adapts LangChain’s functionality to be more compatible with Pega’s JavaScript environment.
  • If possible, implement the LLM integration directly using the APIs that LangChain is using under the hood, as this might be more straightforward in the Pega environment.

References: