<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chris Wan</title>
    <description>The latest articles on DEV Community by Chris Wan (@ocleo1).</description>
    <link>https://dev.to/ocleo1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1135208%2Fb83d59d3-df2b-411c-948e-2c108e4707da.png</url>
      <title>DEV Community: Chris Wan</title>
      <link>https://dev.to/ocleo1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ocleo1"/>
    <language>en</language>
    <item>
      <title>Running LangChain ReactAgent in browser</title>
      <dc:creator>Chris Wan</dc:creator>
      <pubDate>Tue, 11 Nov 2025 15:18:58 +0000</pubDate>
      <link>https://dev.to/ocleo1/running-langchain-reactagent-in-browser-24ik</link>
      <guid>https://dev.to/ocleo1/running-langchain-reactagent-in-browser-24ik</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;LangChain is the easiest way to start building agents and applications powered by LLMs. It provides a pre-built agent architecture and model integrations to help you get started quickly and seamlessly incorporate LLMs into your agents and applications.&lt;/p&gt;

&lt;p&gt;———— LangChain v1.0 is now available!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;The createAgent() function provides a production-ready agent implementation that’s remarkably simple to use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createAgent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;langchain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createAgent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gpt-5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;tools&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, when attempting to use this function within a useEffect() hook in a Next.js project, I encounter a frustrating error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tdza0lvmu0dz4gcb7m2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tdza0lvmu0dz4gcb7m2.png" alt="does not support external modules (request: node:async_hooks)" width="800" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upon investigation, the root cause becomes clear. The &lt;code&gt;context.js&lt;/code&gt; file contains the line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AsyncLocalStorage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node:async_hooks&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unfortunately, &lt;code&gt;node:async_hooks&lt;/code&gt; is not supported in browser environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Import Traces
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Import traces:
  Client Component Browser:
    ./node_modules/@langchain/core/dist/context.js [Client Component Browser]
    ./node_modules/langchain/dist/index.js [Client Component Browser]
    ./apps/web/app/page.tsx [Client Component Browser]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;langchain/dist/index.js&lt;/code&gt; exports &lt;code&gt;context.js&lt;/code&gt;. For now &lt;code&gt;createAgent()&lt;/code&gt; function is what we need only. Can we simply import the agent directly using &lt;code&gt;import { createAgent } from 'langchain/agents'&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Unfortunately, this approach doesn’t work because LangChain’s package.json defines specific exports, preventing other sub-path imports.&lt;/p&gt;

&lt;p&gt;Does LangChain must be used in Node.js runtime environment?&lt;/p&gt;

&lt;p&gt;The answer is no — with the right configuration, LangChain can indeed run in browser environments. I found two ways to do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution I: Resolve Alias Configuration
&lt;/h2&gt;

&lt;p&gt;The first and most elegant solution involves creating a mock for &lt;code&gt;async_hooks&lt;/code&gt; and configuring build system to use it instead of the Node.js module.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Mock async_hooks
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MockAsyncLocalStorage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@langchain/core/singletons&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AsyncLocalStorage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;MockAsyncLocalStorage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AsyncLocalStorage&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also implement a custom &lt;code&gt;AsyncLocalStorage&lt;/code&gt; class, as long as it adheres to the &lt;code&gt;AsyncLocalStorageInterface&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For CommonJS, using &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;module.exports&lt;/code&gt; instead of ES6 imports.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Configure next.config.js
&lt;/h3&gt;

&lt;p&gt;Update Next.js configuration to work with either Webpack or Turbopack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;fileURLToPath&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:url&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;__filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fileURLToPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;__dirname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__filename&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/** @type {import('next').NextConfig} */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nextConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;webpack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;webpack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isServer&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isServer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;webpack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NormalModuleReplacementPlugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/^node:async_hooks$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;async_hooks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;turbopack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;resolveAlias&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:async_hooks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;browser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./async_hooks.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important Note:&lt;/strong&gt; If &lt;code&gt;package.json&lt;/code&gt; includes &lt;code&gt;"type": "module"&lt;/code&gt;, the built-in &lt;code&gt;__dirname&lt;/code&gt; variable won't be available. We need to create our own implementation. For CommonJS, stick with &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;module.exports&lt;/code&gt;, and remove any &lt;code&gt;__dirname&lt;/code&gt;-related logic.&lt;/p&gt;

&lt;p&gt;This configuration is pretty straightforward, it substitutes &lt;code&gt;node:async_hooks&lt;/code&gt; with mocked async_hooks during transpiling.&lt;/p&gt;

&lt;p&gt;To achieve more precise control, we can enhance the configuration using &lt;code&gt;isServer&lt;/code&gt; and &lt;code&gt;browser&lt;/code&gt; conditional aliasing when Webpack and Turbopack target browser environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution II: Source Code Modification
&lt;/h2&gt;

&lt;p&gt;When configuration changes aren’t feasible, we can take a more direct approach by modifying the LangChain source code locally.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Remove export &lt;code&gt;@langchain/core/context&lt;/code&gt; from index.ts&lt;/li&gt;
&lt;li&gt;Change import &lt;code&gt;@langchain/langgraph&lt;/code&gt; to &lt;code&gt;@langchain/langgraph/web&lt;/code&gt; in every file&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While this method is less elegant than the alias configuration approach, it’s particularly suitable for environments where modifying Webpack or Turbopack configurations isn’t possible or practical.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Running langchain agent in browser is cool. However, there’s one crucial security consideration that developers must address: &lt;strong&gt;LLM API key management&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When running LLM-powered applications in the browser, your API keys are exposed to client-side code, creating a significant security vulnerability. Direct API key usage puts your credentials at high risk of being compromised.&lt;/p&gt;

&lt;p&gt;Before deploying any browser-based LangChain implementation, it’s essential to build a &lt;strong&gt;token exchange service&lt;/strong&gt;. It will provide frontend application with temporary, scoped tokens.&lt;/p&gt;

&lt;p&gt;By this, we can safely harness the power of LangChain agents in browser environments while maintaining the security of your LLM service credentials.&lt;/p&gt;

</description>
      <category>langchain</category>
      <category>langgraph</category>
      <category>ai</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
