How to Use Node.js with LangChain (Step-by-Step Guide)

Ever wondered how developers are creating AI-powered apps that can talk, reason, and even automate workflows?
The answer often lies in LangChain a powerful framework that simplifies building applications around LLMs (Large Language Models).
And the best part? You can use it right inside Node.js 🚀
In this post, I’ll walk you through how to use LangChain with Node.js, explain why it’s powerful, and share a quick example to get you started.
🧠 What is LangChain?
LangChain is an open-source framework that helps you connect LLMs like OpenAI, Anthropic, or Gemini with data sources, APIs, and workflows.
Think of it as a bridge between your model and your logic layer enabling features like:
Chaining multiple LLM calls together
Working with APIs, databases, or files
Memory and context management
Agent-based reasoning
It’s the foundation behind many AI apps you see today from chatbots to data assistants.
⚙️ Setting up Node.js + LangChain
Let’s start from scratch 👇
1️⃣ Initialize your Node.js project
mkdir langchain-node-demo
cd langchain-node-demo
npm init -y
2️⃣ Install LangChain and OpenAI SDK
npm install langchain openai dotenv
3️⃣ Setup environment variables
Create a .env file:
OPENAI_API_KEY=your_openai_api_key_here
And load it in your code:
import dotenv from "dotenv";
dotenv.config();
🧩 Writing Your First LangChain Script
Here’s a simple example using OpenAI’s model through LangChain in Node.js:
import { ChatOpenAI } from "langchain/chat_models/openai";
import { HumanMessage } from "langchain/schema";
import dotenv from "dotenv";
dotenv.config();
const model = new ChatOpenAI({
openAIApiKey: process.env.OPENAI_API_KEY,
modelName: "gpt-3.5-turbo",
temperature: 0.7,
});
async function run() {
const response = await model.call([
new HumanMessage("Explain JavaScript closures in simple terms."),
]);
console.log(response.content);
}
run();
✅ What’s happening here?
You import a model wrapper from LangChain.
Pass your API key securely via
.env.Send a message as a HumanMessage, and LangChain handles everything behind the scenes model connection, response parsing, and more.
🧠 Going Beyond: Chains and Memory
LangChain isn’t just about single queries it’s about chains of logic.
For example, you can:
Create a Sequential Chain where one model’s output becomes another’s input.
Add Memory to maintain chat history.
Combine LLMs with tools like web search, APIs, or databases.
A simple chain example 👇
import { LLMChain } from "langchain/chains";
import { PromptTemplate } from "langchain/prompts";
const prompt = new PromptTemplate({
template: "Write a tweet about {topic} in a funny tone.",
inputVariables: ["topic"],
});
const chain = new LLMChain({ llm: model, prompt });
const result = await chain.call({ topic: "JavaScript developers" });
console.log(result.text);
💡 Output Example:
“JavaScript developers be like: I’ll fix it later... but later never comes 😅 #CodingLife”
🧰 Real-World Use Cases
You can build a lot with Node.js + LangChain combo:
AI Chatbots with memory and personality
Automated content generation tools
Data-driven assistants that connect to APIs or databases
Code helpers or dev documentation bots
🚀 Final Thoughts
LangChain + Node.js is a game-changer for JavaScript developers who want to step into AI app development without switching stacks.
📌 My advice:
Start small build a chatbot or summarizer.
Learn how to combine chains, memory, and tools.
Gradually connect it with your APIs or database.
If you found this useful, share it with your dev friends exploring AI tools.
And comment below If you want step-by-step guide on building an AI chatbot next, let me know in comments 🤖