LangChain.js Tutorial Setup

LangChain.js Tutorial Setup

Environment setup for LangChain.js


3 min read


  • Node.js
    This tutorial requires that you have Node.js installed. Node.js is the Javascript compiler/interpreter that is used to run Javascript applications. If you do not have Node.js installed, install Node.js from the Node.js installation site here

  • Install the NPM package and project manager

    If you do not have NPM already installed, you can install it from here


  • Create the tutorial project folder

      $ mkdir langchainjs-tutorial-1
      $ cd langchainjs-tutorial-1
  • Initialize your project as an npm project and create the package.json project configuration file
    (The es6 flag tells npm to create a package.json with the "type": "module" setting. The -y flag tells npm to accept the defaults)

      $ npm init es6 -y
  • Install the TypeScript Node package. Configures Node to run TypeScript code.

      $ npm install typescript
  • Configure capability to run Typescript code using Node commands

      $ npm install -D ts-node
  • Add the type definitions for Node to run TypeScript

      $ npm install @types/node
  • Create the tsconfig.json that contains the information required to compile TypeScript to JavaScript

      $ npx tsc --init --rootDir src --outDir ./dist --esModuleInterop --lib ES2020 --target ES2020 --module nodenext --noImplicitAny true
  • Add build and run scripts to package.json. Replace "scripts" entry with the JSON below. If you do not have a "scripts" entry in your package.json, place the JSON below above dependencies (or devDependencies)

      "scripts": { 
          "build": "tsc", 
          "start": "node ./dist/app.js", 
          "dev": "ts-node --esm ./src/app.ts"
  • Create an app.ts source file in the src folder. Add code to it to display a test string.

    (if the "echo" command does not work for you, simply create app.ts in the src folder and cut and paste the "console.log..." statement)

      $ mkdir src 
      $ echo "console.log('Welcome to the LangChain.js tutorial by LangChainers.')" > src/app.ts
  • Perform a test to verify that the tutorial project can build and run using "yarn"

      $ npm run build
      $ npm run start

    You can also use $ npm run dev for a single command that executes both of the above.

  • If everything works, you are ready for some LanngChain.js. LET'S GO!!


  • Install the LangChain.js package

      $ npm install langchain
  • Install the OpenAI package

      $ npm install openai
  • Install the dotenv package for managing environment variables including your OpenAI API key

      $ npm install dotenv
  • Create a file named .env in the project directory and add your OpenAI API Key to the file as shown below
    (if you do not have an OpenAI API Key, go to your OpenAI API Key page, then cut and paste the OpenAI API Key below)

      OPENAI_API_KEY=<remove the < and > and put your OpenAI API key here>

    Now your environment is ready for LangChain.js tutorials.