docx

Generate and modify Microsoft Word .docx files with a declarative JS/TS API in Node or the browser.

Library
npm
v9.7.1
5,884stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
89/100Excellent
Development Activity88
Maintenance96
Community72
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
79/100Good
Architecture80
Code Quality84
Innovation74
Learning Curve78

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 Packer utility 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search