next.jsartificial-intelligencelangchain

How to fix 'fs module not found' error when using Langchain document loaders in Next.js?


I am working on an AI project. I am using Langchain and Next.js 13.

I am trying to use the document loaders in langchain to load my PDF, however when I call a loader eg

import { PDFLoader } from "langchain/document_loaders/fs/pdf";

Immediately I get an error:

fs module not found

As per langchain documentation, this should not occur as it states that the APIs support Next.js enviroment.

I have tried using a package(pdf.js) to handle PDF file upload and parsing, but I get an error with this as well.

How can I use langchain document loaders in Next.js?


Solution

  • I finally understand the issue here.

    I was calling it in the browser context but once I moved my code to the API routes(Node) environment, everything worked as expected.