docx
Generate and modify Microsoft Word .docx files with a declarative JS/TS API in Node or the browser.
Repository Health
Technical Analysis
docx is a JavaScript/TypeScript library for programmatically generating and modifying Microsoft Word (.docx / OOXML) documents, running identically in Node.js and the browser. Instead of hand-assembling OOXML XML, developers compose documents from typed building blocks — Document, Paragraph, TextRun, Table, headers/footers, images, and more — and hand the result to Packer to produce a real .docx file.
Beyond basic document creation, docx supports advanced Word features such as styles, numbering, tables of contents, footnotes, comments, and a document patcher for editing existing .docx templates in place. With 5,800+ GitHub stars, 146 contributors, and active maintenance, it is the de facto standard for Word document generation in the JavaScript ecosystem, used for everything from invoice generation to reporting pipelines.
What You Get
- A declarative document model (
Document,Paragraph,TextRun,Table,ImageRun, headers/footers) that maps closely to how Word documents are structured - Isomorphic support — the same API runs in Node.js and directly in the browser without a native dependency
- Rich formatting support: styles, numbering/bullets, tables of contents, footnotes, comments, page breaks, and sections
- A
Packerutility to serialize documents to Buffer, Blob, base64, or stream depending on the target environment - A document patcher (
patchDocument) for modifying fields inside an existing .docx template without rebuilding it from scratch - Full TypeScript typings and extensive docs/demo scripts covering common document-construction patterns
Common Use Cases
- Generating invoices, contracts, or reports as downloadable .docx files from a web app or backend service
- Building document-automation pipelines that merge data into Word templates (e.g. certificates, letters, statements)
- Producing .docx exports as an alternative to PDF from reporting or CMS-style applications
- Patching specific fields or placeholders inside existing Word templates without regenerating the whole document
Under The Hood
Architecture - The library models a Word document as a tree of typed classes under src/file (Document/File, Paragraph, TextRun, Table, sections, headers/footers, numbering, styles), each of which knows how to render itself to OOXML XML. src/export contains the Packer, which walks the document tree, serializes each part to its corresponding XML file, and zips them into the final .docx (a .docx is itself a ZIP of XML parts per the OOXML spec). A separate src/patcher module implements string/placeholder-based patching of an existing .docx’s XML parts, letting callers modify a template without reconstructing the whole tree.
Tech Stack - Written in TypeScript, built and bundled with Vite into dual ESM/CJS output plus type declarations, and tested with Vitest (including a coverage/UI mode). It ships as a pure JS/TS package with no native bindings, which is what allows it to run unmodified in both Node and browser bundlers.
Code Quality - The src tree includes 196 *.spec.ts files, giving dense unit coverage across nearly every document-model class (paragraphs, tables, styles, numbering, patcher, etc.). Code is organized by OOXML concern (file/, export/, patcher/, util/) with consistent naming that mirrors Word’s own terminology, ESLint and Prettier enforce style, and cspell checks catch documentation typos — indicating a maintained, quality-conscious codebase rather than a one-off generator.
API Design - The class-based, declarative API (new Document({ sections: [{ children: [new Paragraph(...)] }] })) closely tracks how a Word document is conceptually structured, which makes the learning curve shallow for anyone who has used Word. Extensive docs (a docsify site under docs/), a demo/ directory with runnable examples for dozens of features, and full TypeScript types substantially lower the boilerplate needed to produce a first working document.
Used by 4 apps in this directory
AionUi
AI Agents · Productivity
Free, open-source Cowork desktop app that unifies Claude Code, Codex, Gemini CLI, and 20+ AI agents into a single platform with multi-agent teams, 24/7 cron automation, and zero-config built-in agent.
AnythingLLM
Developer Tools · Automation · AI Assistants
The all-in-one AI platform for private document chat, no-code agents, and local LLMs with zero setup friction.
Cherry Studio
AI Assistants
All-in-one AI desktop client with 300+ assistants and multi-model support
Docmost
Productivity · Note Taking · Collaboration
Self-hosted collaborative wiki and knowledge base with real-time editing, diagrams, AI assistance, and enterprise access controls — a modern alternative to Confluence and Notion.