* fix: patch placeholders in links, content controls and tracked changes patchDocument and patchDetector only read runs that are direct children of a paragraph, so a placeholder in a link, a content control, text typed with Track Changes on, a smart tag or a field's result was left as it was, with no error. Runs in those elements are now read and patched where they are. Where the content can't go in what holds the placeholder, it goes where Word can open it: a link is taken out of a tracked insertion (whose piece after it gets an id of its own) or a link, a plain text content control given more than text becomes a rich text one, and a content control showing its placeholder text stops showing it. Addresses #2679 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: cover every branch of patching runs in links and content controls CI's coverage fell below 100%: an empty link, a content control without properties, and runs that stay together when a tracked insertion is split weren't tested. The name fallbacks in the replacer's lookups could never be reached, so the lookups take an element's name as it is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * feat: find placeholders with custom delimiters with patchDetector, and say why a placeholder isn't replaced patchDetector takes placeholderDelimiters, as patchDocument does, so it finds the same placeholders. Before, it only knew {{ and }}, and found nothing in a template with <<name>>. templates.md has a section on a placeholder that isn't replaced: how to find the patches whose placeholder isn't in the template with patchDetector, as patchDocument leaves them out without an error, and the usual reasons, such as a key written with its braces. Addresses #2679 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: unbind a content control a patch fills, so Word doesn't put the placeholder back Word fills a control bound to custom XML or a document property (w:dataBinding) in from its data when it opens a document. A patched placeholder in one came back: Word for Mac showed {{name}} from the XML, though the control held the patch's text. The binding is now removed from a control a patch's content goes in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * perf: split a tracked change around a link in linear time The stretches were built by copying the stretch so far for each element, so a tracked insertion of n runs took n² steps to split. Each stretch is now sliced from where it starts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * docs: list a placeholder only in alt text among the reasons a patch isn't applied patchDetector finds placeholders in pictures' and charts' alt text, so comparing its keys with the patches' doesn't show a text patch for one, which leaves it as it is. It's now one of the listed reasons, saying so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: don't let a start delimiter before a placeholder hide it from patchDetector A key ran from the first start delimiter to the next end delimiter, so in "<<unfinished <<name>>" patchDetector found "unfinished <<name", not "name", which patchDocument patches. With one-character delimiters such as < and >, any "<" in the text did this. A key now has no start delimiter in it. The search from a start delimiter with no end after it also stops at the next one, rather than going on to the end of the paragraph each time: 120,000 characters of unmatched "<" took 1.4 s, and now take no time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * test: check the link a patch puts in a content control without properties The test only checked the control's text and its one child, so content that lost the link would have passed it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Easily generate and modify .docx files with JS/TS. Works for Node and on the Browser.
Demo
Browser
With basic HTML/JS in a browser, load docx's browser build, which defines a global docx:
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/docx@9/dist/index.iife.js"></script>
</head>
<body>
<button type="button" onclick="generate()">Download a Word document</button>
<script>
function generate() {
const doc = new docx.Document({
sections: [
{
children: [
new docx.Paragraph({
children: [new docx.TextRun("Hello World"), new docx.TextRun({ text: "Foo Bar", bold: true })],
}),
],
},
],
});
docx.Packer.toBlob(doc).then((blob) => {
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "example.docx";
link.click();
});
}
</script>
</body>
</html>
Here are examples of docx working in Angular:
Here are examples of docx working in React:
- https://stackblitz.com/edit/react-docx
- https://stackblitz.com/edit/react-docx-images (adding images to Word Document)
Here is an example of docx working in Vue.js:
Node
Press endpoint on the RunKit website:
- https://runkit.com/dolanmiu/docx-demo1 - Simple paragraph and text
- https://runkit.com/dolanmiu/docx-demo2 - Advanced Paragraphs and text
- https://runkit.com/dolanmiu/docx-demo3 - Bullet points
- https://runkit.com/dolanmiu/docx-demo4 - Simple table
- https://runkit.com/dolanmiu/docx-demo5 - Images
- https://runkit.com/dolanmiu/docx-demo6 - Margins
- https://runkit.com/dolanmiu/docx-demo7 - Landscape
- https://runkit.com/dolanmiu/docx-demo8 - Header and Footer
- https://runkit.com/dolanmiu/docx-demo10 - My CV generated with docx
More here
How to use & Documentation
Please refer to the documentation at https://docx.js.org/ for details on how to use this library, examples and much more!
Shapes, watermarks and charts
Shapes, groups of shapes and diagrams with connectors come with docx, from docx/shapes, watermarks from docx/watermarks, and native Word charts from docx/charts. They are left out of docx itself, so documents that don't use them don't carry their code:
import { Document, Header, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
import { ShapeRun } from "docx/shapes";
import { TextWatermark } from "docx/watermarks";
const doc = new Document({
sections: [
{
headers: { default: new Header({ children: [new Paragraph({ children: [new TextWatermark({ text: "DRAFT" })] })] }) },
children: [
new Paragraph({ children: [new ShapeRun({ type: "ellipse", transformation: { width: 100, height: 60 } })] }),
new Paragraph({
children: [new ChartRun({ type: "column", categories: ["Q1", "Q2"], series: [{ name: "Sales", values: [10, 20] }] })],
}),
],
},
],
});
In a page without a bundler, load dist/shapes.umd.cjs, dist/watermarks.umd.cjs or dist/charts.umd.cjs after dist/index.umd.cjs. They add the docxShapes, docxWatermarks and docxCharts globals. See the shapes, watermarks and charts documentation.
HTML
HTML, such as what people write in a rich-text editor, becomes a document's paragraphs, lists, tables, images, links, equations and footnotes with docx/html, written in the document's own styles:
import { Document } from "docx";
import { htmlToContent } from "docx/html";
const doc = new Document({
sections: [{ children: htmlToContent("<h1>Report</h1><p>Sales were <b>up 12%</b>.</p><ul><li>EMEA</li><li>APAC</li></ul>") }],
});
In a page without a bundler, load dist/html.umd.cjs after dist/index.umd.cjs and dist/math.umd.cjs. It adds the docxHtml global. See the HTML documentation.
Macros
VBA macros, written from their code, make a document a macro-enabled document (.docm), with docx/macros:
import { Document, Paragraph } from "docx";
import { createMacros } from "docx/macros";
const doc = new Document({
macros: createMacros({
modules: [{ name: "Greetings", code: 'Sub SayHello()\n MsgBox "Hello"\nEnd Sub' }],
}),
sections: [{ children: [new Paragraph("Run SayHello from View > Macros")] }],
});
In a page without a bundler, load dist/macros.umd.cjs after dist/index.umd.cjs. It adds the docxMacros global. See the macros documentation.
Embedded Files
Workbooks, documents, presentations and any other file, such as a sound, a video or a PDF, are embedded in a document as OLE objects, which open from it, with docx/embedded-files:
import * as fs from "fs";
import { Document, Paragraph } from "docx";
import { EmbeddedFileRun } from "docx/embedded-files";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new EmbeddedFileRun({
fileName: "budget.xlsx",
data: fs.readFileSync("./budget.xlsx"),
preview: { type: "png", data: fs.readFileSync("./budget.png") },
transformation: { width: 240, height: 120 },
}),
],
}),
],
},
],
});
In a page without a bundler, load dist/embedded-files.umd.cjs after dist/index.umd.cjs. It adds the docxEmbeddedFiles global. See the embedded files documentation.
Citations and Bibliography
Sources, citations of them in the text and a bibliography, in one of Word's styles of citation, such as APA, MLA or Chicago, written as Word writes them, with docx/bibliography:
import { Document, Paragraph, TextRun } from "docx";
import { Bibliography, Citation, CitationStyle, SourceType, createBibliography } from "docx/bibliography";
const doc = new Document({
bibliography: createBibliography({
style: CitationStyle.APA,
sources: [
{
tag: "Bak10",
type: SourceType.BOOK,
authors: [{ lastName: "Baker", firstName: "Anna" }],
title: "The Art of Pizza",
year: 2010,
city: "Naples",
publisher: "Oven Press",
},
],
}),
sections: [
{
children: [
new Paragraph({ children: [new TextRun("Pizza has been baked in Naples for centuries "), new Citation("Bak10")] }),
new Bibliography({ title: "References" }),
],
},
],
});
In a page without a bundler, load dist/bibliography.umd.cjs after dist/index.umd.cjs. It adds the docxBibliography global. See the citations and bibliography documentation.
Playground
Experience docx in action through Docx.js Editor, an interactive playground where you can code and preview the results in real-time.
Examples
Check the demo folder for examples.
Contributing
Read the contribution guidelines here.
Used by
...and many more!
Made with 💖


















