DocxLoader parses DOCX files into LangChain.js Document objects, preparing their content for chunking, summarization, or information retrieval. Import it from @langchain/community/document_loaders/fs/docx and run it in a Node.js or browser-based JavaScript application. section 7.1.3
Load a DOCX file
Select the loader from the file extension, instantiate it with the path, and call load(). The returned documents can then flow into a text splitter or summarization chain. section 7.1.3
Listing 7.1 Loading different document types
import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf"; #1
import { DocxLoader } from "@langchain/community/document_loaders/fs/docx";
import path from "path"; #2
const loaders = {
pdf: PDFLoader,
docx: DocxLoader,
};
const loadDocumentFromFile = async (filePath) => { #3
const fileExtension = path.extname(filePath)
.substring(1).toLowerCase();
const LoaderClass = loaders[fileExtension];
if (!LoaderClass) {
console.error(`No loader found for file extension: ${fileExtension}`);
return null;
}
const loader = new LoaderClass(filePath);
return await loader.load();
};
loadDocumentFromFile("./data/aesops-fables.pdf") #4
.then((documents) => {
console.log(`Loaded ${documents.length} documents.`);
})
.catch((error) => {
console.error("Error loading documents:", error);
});
Use the complete loader pattern to support DOCX alongside PDF files, handle unsupported extensions, and report loading errors. section 7.1.3
