
LANGCHAIN — LangChainJS now supports running in various JavaScript environments
Digital design is like painting, except the paint never dries. — Neville Brody
LangChainJS now supports running in various JavaScript environments such as browsers, Deno, Cloudflare Workers, Vercel/Next.js, and more. Here’s a brief overview of the changes made to enable this support.
Changes to Enable Multiple Environments
To enable support for multiple JavaScript environments, several changes were made to the LangChainJS codebase:
Conversion to ESM
The codebase was converted to ECMAScript Modules (ESM) to support modern JavaScript environments while still offering a CommonJS (CJS) build for compatibility.
import { LLMChain } from "langchain/chains";Removal of Node-only APIs
Node-only APIs were removed where possible to ensure compatibility across different environments.
Use of fetch for Streaming and Batch OpenAI Requests
Streaming and batch OpenAI requests were modified to use the fetch API to facilitate running in browsers and other environments.
Updates to Exports
The exports were updated to better support optional dependencies and produce smaller bundles, ensuring that unnecessary code is not included.
Code Changes for Multiple Environments
The way LangChain is used has been updated to address concerns about bundle size and optional dependencies when running in different environments:
Old way:
import { LLMChain } from "langchain/chains";
import { PromptTemplate } from "langchain/prompts";
import { OpenAI } from "langchain/llms";
import { SupabaseVectorStore } from "langchain/vectorstores";
import { CohereEmbeddings } from "langchain/embeddings";
import { GithubRepoLoader } from "langchain/document_loaders";Updated way:
import { LLMChain } from "langchain/chains";
import { PromptTemplate } from "langchain/prompts";
import { OpenAI } from "langchain/llms/openai";
import { SupabaseVectorStore } from "langchain/vectorstores/supabase";
import { CohereEmbeddings } from "langchain/embeddings/openai";
import { GithubRepoLoader } from "langchain/document_loaders/web/github";Breaking Changes and Deprecations
A few breaking changes and deprecations were introduced to enable support for multiple environments. Developers using LangChain should be aware of these changes, such as updates to import paths for specific functionalities.
Testing for Compatibility
To ensure that LangChainJS remains compatible with various JavaScript environments, rigorous testing is conducted. This includes creating test projects for different environments, setting up build and test scripts, and testing in isolated environments using CI/CD pipelines.
If you encounter any issues running LangChainJS in a particular environment or have suggestions for additional environments to test, the LangChain team welcomes feedback and contributions.
In conclusion, by enabling support for multiple JavaScript environments, LangChainJS is now more versatile and adaptable to different runtime environments, offering a seamless experience for developers across various platforms.






