Installing LangChain.js in VS Code involves setting up a Node.js project and using
or to add the necessary packages. The process is the same as installing any other JavaScript library.
Prerequisites
Before you begin, ensure you have the following installed on your system:
- Node.js: LangChain.js runs on Node.js, and it includes the (Node Package Manager) command-line tool.
- VS Code: Your preferred code editor. [3]
- Open VS Code and create a new project folder.
- Open the integrated terminal in VS Code (Terminal > New Terminal or ++).
- Initialize a new Node.js project by running this command in the terminal. This creates a file:
- Install the main LangChain.js package using (or or ):
$ npm install langchain
- Alternatively, use :
$ yarn add langchain
- Install specific integrations as needed. LangChain uses a modular design, so install packages for the specific Large Language Models (LLMs) or tools planned to be used
5. Get a Google API Key: Obtain a Gemini API key from Google AI Studio.
6. Set Environment Variable: Set the API key as an environment variable named GOOGLE_API_KEY:
> setx GOOGLE_API_KEY "your-google-gemini-api-key" //In windows command console.
OR
> [Environment] :: SetEnvironmentVariable("GOOGLE_API_KEY", "<YOUR_API_KEY_VALUE>", "User")
6. For Google Gemini models:
$ npm install @langchain/google-genai
7. For managing API keys securely using environment variables, install :
$ npm install dotenv
8. Configure the project for ES Modules (ESM) by adding to the file. This allows the use of statements:
To configure a project for ES Modules (ESM), the primary method is adding to your , which makes all files use ESM syntax (/); alternatively, use the extension for specific files or the flag for direct execution, but ensure you update CommonJS patterns (like , ) to ESM equivalents (, ), replacing with dynamic imports for path resolution, explains W3Schools, YouTube, and DEV Community. [1, 2, 3, 4]
Common Configuration Methods
- in (Recommended for ESM-first)
- Add to your file.
- This makes files ESM by default.
- For any files that must remain CommonJS (CJS), rename them to or use for ESM files.
// package.json
{
"name": "my-esm-project",
"version": "1.0.0",
"type": "module" // <--- This line enables ESM
}
- .mjs Extension (For mixed projects)
- Use the .mjs extension for individual files you want to run as ES Modules.
- This works even if package.json is set to "type";"commonjs" .
- Node.js Command-Line Flag
Code Migration Steps
- Imports: Change const fs = require('fs') to import fs from 'fs' or
import { readFile } from 'fs'. - Exports: Replace module.exports = { ... } with export { ... } or
export default { ... }. - Paths: Add file extensions to relative imports, e.g., import util from './util.js' .
- Globals: Remove
__dirnameand __filename ; use import.meta.url to get the current module's URL and construct paths. - :
'use strict' :Can be removed as ESM files are strict by default. [2, 4, 6, 7]
Web Browser Configuration
- Add type="module" to your <script> tag to load an ES Module.
- Use <script> attribute for fallback scripts in older browsers. [3]
<script type="module" src="app.js"></script>
<script nomodule src="legacy-app.js"></script>
AI responses may include mistakes.
For the best development experience with LangChain.js, using TypeScript is recommended.
- Install TypeScript and Node.js type definitions:
- Create a file in the project root:
- Create a source file in a new directory, for example, .
- Add sample code to the file and run it using . [3, 8, 9, 10, 11]
Once these steps are completed, the VS Code environment is set up and ready for LangChain.js development.
Now, do 'add, commit and push' git bash ops to push this change to your remote github repo:
$ git add .
$ git commit -m "coment"
$ git push -u origin main
Now see the new updates actually happened in your github repo
AI responses may include mistakes.
[8] https://medium.com/@ashithvl/langchain-js-for-javascript-developers-the-ultimate-guide-dbdf3ff8c1d0
.png)
.png)
.png)
.png)
.png)
No comments:
Post a Comment