Parse and Read DOCX Files with mammoth.js — Node.js Tutorial
Extract text and HTML from Microsoft Word DOCX files in Node.js using mammoth.js. Covers style mapping, custom transforms, and testing with 1MB–50MB DOCX samples.
Parsing Microsoft Word (.docx) documents into clean HTML or Markdown is essential for content management systems, resume parsers, and blog importers. The mammoth.js library converts OpenXML documents with a focus on semantic HTML elements.
Installing mammoth
npm install mammoth
Download our 10MB sample DOCX document containing formatting, tables, and headers to test Mammoth.
Download 10MB DOCX Sample →Extracting Semantic HTML and Markdown in Node.js
import mammoth from 'mammoth';
import fs from 'fs';
export async function convertDocxToHtml(filePath: string): Promise<string> {
const buffer = fs.readFileSync(filePath);
const result = await mammoth.convertToHtml({ buffer }, {
styleMap: [
"p[style-name='Heading 1'] => h1:fresh",
"p[style-name='Heading 2'] => h2:fresh",
"p[style-name='Lead'] => p.lead:fresh",
"r[style-name='Emphasis'] => em",
],
});
if (result.messages.length > 0) {
console.log('Mammoth conversion warnings:', result.messages);
}
return result.value; // The rendered HTML string
}
export async function extractRawDocxText(filePath: string): Promise<string> {
const buffer = fs.readFileSync(filePath);
const result = await mammoth.extractRawText({ buffer });
return result.value;
}
Parsing DOCX in the Browser
Mammoth runs natively in the browser using FileReader:
import mammoth from 'mammoth';
export async function parseDocxFileInBrowser(file: File): Promise<string> {
const arrayBuffer = await file.arrayBuffer();
const { value: html } = await mammoth.convertToHtml({ arrayBuffer });
return html;
}
Advantages of Mammoth Over Raw Regex
- Semantic structure: Converts Word paragraph styles to true
<h1>,<p>, and<ul>tags instead of inline styling spans. - Safe HTML output: Automatically sanitizes output and encodes special characters to prevent stored XSS attacks.
Nguyen Dai Long
AuthorBackend Lead • Distributed Systems & Cloud Edge Architecture Specialist
4+ years designing high-throughput file ingestion pipelines, database architectures, and distributed edge storage on Cloudflare R2 & AWS S3. Founder of FileDummy and the NDL Ecosystem.
Was this article helpful?
Click Like to support the author and help other developers discover this guide.
Engineering Discussion & Feedback (0)
Share benchmark results, report edge cases, or ask technical questions.
No comments yet. Be the first developer to start the discussion!
Frequently Asked Questions
What is mammoth.js and what does it do?▾
mammoth.js converts DOCX files to HTML or plain text. It reads the OpenXML ZIP structure, maps Word styles (Heading 1 → h1, Bold → strong) to HTML, and outputs clean semantic HTML.
Does mammoth.js preserve tables and images?▾
Tables are converted to HTML table elements. Embedded images can be extracted as base64 data URIs using a custom convertImage handler in the options. Unsupported formatting is silently ignored.
What alternatives to mammoth.js exist for DOCX parsing?▾
Alternatives include docxtemplater (commercial), LibreOffice headless conversion, and Apache POI (Java). For DOCX creation, use the docx npm package.