Hello! In our previous lesson, we built a conceptual map of the LangChain framework, identifying its key components and the problems it solves. We looked at everything from models and prompts to the more advanced concepts of agents and memory.
Today, we transition from theory to practice. This lesson is all about rolling up our sleeves and setting up a proper development environment. By the end, you will have a fully configured TypeScript project with LangChain.js installed, ready for us to start building in the lessons to come. We'll initialize a project, install the necessary packages, configure TypeScript, and learn how to manage API keys securely.
The Modular World of LangChain.js
Before we start typing npm install, it's important to understand how LangChain.js is structured. Unlike a single, monolithic library, LangChain is a collection of packages. This modular design is intentional. It allows you to install only what you need, keeping your project lightweight.
The most important package is @langchain/core. It contains the fundamental abstractions and the LangChain Expression Language (LCEL) that we will use to compose our applications. Other packages provide integrations with specific services, such as @langchain/openai for OpenAI models, @langchain/cohere for Cohere models, or @langchain/community for a wide range of community-contributed components.
The image below shows the internal directory structure of the @langchain/core package. You might recognize some of the folder names—like runnables, output_parsers, and language_models—from the components we discussed in our last lesson. This is where the core logic for those abstractions lives.

As we move forward, most of our installations will involve pulling in @langchain/core (often as a dependency of the main langchain package) along with one or more integration packages.
Setting Up Your TypeScript Project
Now, let's build our project. We will follow a structured process to create a robust foundation for our work in this course. The article "Build Your First LLM Application with LangChain & TypeScript" provides an excellent step-by-step guide that we will use as our primary reference.
Build Your First LLM Application with LangChain & TypeScript
This article by Dhananjay Kumar on the Telerik blog will walk us through initializing a Node.js project, setting up TypeScript, and configuring environment variables. While the article is excellent, some package names have evolved. I will provide the most current npm commands, but you should use the article for the configuration files and overall process.
Please read the sections Set Up the Project and Set Up the Environment. As you follow along, pay close attention to the tsconfig.json and the setup of the .env file. We will execute these steps together now.
Let's walk through this process together, step-by-step.
1. Initialize Your Project
First, create a new directory for your project on your local machine, navigate into it with your terminal, and initialize a new Node.js project.
mkdir langchain-project
cd langchain-project
npm init -y
This creates a package.json file, which will track our project's dependencies.
2. Install Dependencies
Now we'll install the necessary packages. We need langchain and the OpenAI integration, dotenv for managing environment variables, and the TypeScript compiler and its related tools as development dependencies.
Execute the following commands in your terminal:
# Install runtime dependencies
npm install langchain @langchain/openai dotenv
# Install development dependencies
npm install -D typescript @types/node ts-node @types/dotenv
This installs all the packages we need for the next few lessons.
3. Configure TypeScript
As instructed in the Telerik article, generate a tsconfig.json file by running:
npx tsc --init
Then, replace the entire contents of the generated tsconfig.json file with the configuration provided in the article. This configuration is optimized for a Node.js environment and tells the TypeScript compiler how to handle our files. For convenience, here is that configuration:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
Next, update your package.json to add the scripts for building and running your application, as shown in the article. This allows you to run npm start to compile and execute your code.
4. Set Up Your Environment
A crucial part of building AI applications is managing API keys. You should never hard-code secret keys directly into your source code. The dotenv library allows us to keep them in a separate, local file.
- Create a
.envfile in the root directory of your project. - Add your OpenAI API key to this file. If you don't have one, you'll need to create an account on the OpenAI Platform and generate a new secret key.
Your .env file should look like this:
OPENAI_API_KEY="sk-..."
Note: The Telerik article also mentions LANGSMITH_API_KEY. LangSmith is a powerful tracing and debugging tool from LangChain. While incredibly useful, we will not be using it just yet to keep our initial setup simple. We will revisit it later in the course.
Finally, create a .gitignore file in your project root and add .env and node_modules/ to it. This prevents you from accidentally committing your secret key or the massive node_modules folder to a git repository.
# .gitignore
node_modules
dist
.env
5. Create Your First TypeScript File
Create a src folder in your project root, and inside it, create a file named index.ts. This is where our application code will live.
Copy the final code snippet from the "Set Up the Environment" section of the Telerik article into your src/index.ts file. This code imports dotenv, loads the environment variables, and prints them to the console to verify that everything is working.
import dotenv from 'dotenv';
dotenv.config();
const openaiApiKey: string | undefined = process.env.OPENAI_API_KEY;
console.log(`Verifying OpenAI API Key: ${openaiApiKey ? 'Loaded' : 'Not Found'}`);
// We will add our LangChain code here in the next lesson.
I've slightly modified the code to give a clearer success message without printing the key itself.
Now, run your application:
npm start
If everything is configured correctly, you should see the output: Verifying OpenAI API Key: Loaded. This confirms that your project is set up, TypeScript is compiling, and your API key is being loaded correctly from the .env file.
Conclusion
Congratulations! You have successfully set up and configured a complete LangChain.js development environment. While it might seem like a lot of boilerplate, this robust setup will serve us well as we build more complex applications.
Key Takeaways:
- LangChain.js has a modular architecture, with a
@langchain/corepackage and various integration packages like@langchain/openai. - A standard TypeScript project for LangChain requires initializing
npm, installing dependencies (langchain,typescript,dotenv), and creating atsconfig.json. - API keys and other secrets should always be stored in a
.envfile and kept out of source control via.gitignore. - You can verify your setup by using
dotenvto load and confirm the presence of your API key.
You now have a solid launchpad. In our next lesson, we will use this setup to initialize and invoke our first chat model, making our first official call to an LLM through the LangChain framework.