betterdocx
v0.11.0
Published
Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
Downloads
471
Maintainers
Readme
Better DOCX
Easily generate and modify .docx files with JS/TS. Works for Node and on the Browser.
A modern fork of docxjs
Installation
npm install betterdocxor:
pnpm install betterdocxor:
bun install betterdocxBetter DOCX supports modern browsers and JavaScript runtimes with Web Platform APIs, plus Node.js 24 and newer.
[!IMPORTANT] This is an ESM-only package — by design. Better DOCX ships ES modules only and intentionally does not provide a CommonJS (
require) build. This is a deliberate decision, not an omission: please do not add a CJS/dual build,main/requireexport conditions, or.cjs/.d.ctsartifacts. Consume it withimport(or a dynamicimport()from CJS on Node 24+). Theengines.nodefloor is>=24, also by design.
Package entries
The root import remains the complete, backwards-compatible API. New code can use the smaller, purpose-specific entries when that makes the dependency boundary clearer:
betterdocx/core— document construction and packing;betterdocx/patcher— template patching, placeholder detection, and style reading;betterdocx/advanced— low-level XML components and formatting extension points.
For example, a generation-only application can import from betterdocx/core, while a
template workflow typically imports document components from betterdocx/core and
patchDocument from betterdocx/patcher. Existing imports from betterdocx continue
to work unchanged.
Quick Start
import { Document, Packer, Paragraph, TextRun } from "betterdocx";
import * as fs from "fs";
const doc = new Document({
sections: [
{
properties: {},
children: [
new Paragraph({
children: [
new TextRun("Hello World"),
new TextRun({
text: "Foo Bar",
bold: true,
}),
],
}),
],
},
],
});
Packer.toUint8Array(doc).then((bytes) => {
fs.writeFileSync("My Document.docx", bytes);
});Documentation
Read the complete documentation including:
- Getting Started - Installation and basic usage
- Usage Guides - Detailed guides for all features
- Exporting - How to generate .docx files
- Modifying Documents - Working with existing files
- Utility Functions - Helper functions and utilities
Examples
Browse the demo source on GitHub for examples.
Contributing
Read the contribution guidelines to get started.
License
MIT
