---
title: "safe-docx vs docx (Dolan Miu) — Word Document Tool Comparison"
description: "Compare safe-docx MCP server with the docx npm package by Dolan Miu. Brownfield editing of existing documents vs greenfield document generation."
permalink: comparisons/safe-docx-vs-docx.html
githubUrl: https://github.com/UseJunior/safe-docx
githubRepo: UseJunior/safe-docx
ctaIntro: "Ready to install safe-docx?"
cta:
  primary:
    text: "Try safe-docx"
    href: "/products/safe-docx/"
comparison:
  featureColumn: "Feature"
  products:
    - "safe-docx"
    - "docx"
  items:
    - feature: "Native MCP server"
      description: "Built-in MCP server callable from Claude Code, Gemini CLI, and other agents"
      values: [true, false]
    - feature: "Edit existing documents (brownfield)"
      description: "Open a .docx file and make formatting-preserving edits"
      values: [true, "partial"]
    - feature: "Smart formatting defaults"
      description: "New content inherits formatting from existing document context automatically"
      values: [true, false]
    - feature: "Document comparison with redline output"
      description: "Compare two document versions and generate a tracked-changes document"
      values: [true, false]
    - feature: "Comments (add/delete)"
      description: "Programmatic margin comment management"
      values: [true, false]
    - feature: "Footnotes (add/update/delete)"
      description: "Programmatic footnote management"
      values: [true, false]
    - feature: "Generate new documents from scratch (greenfield)"
      description: "Create .docx files programmatically — reports, proposals, letters"
      values: [false, true]
    - feature: "Declarative document API"
      description: "Build documents with a structured, declarative JavaScript API"
      values: [false, true]
    - feature: "Open source (Apache-2.0)"
      description: "Permissive license, source code available"
      values: [true, true]
    - feature: "Runs locally"
      description: "No external data transmission"
      values: [true, true]
tags: ["Comparison", "safe-docx", "docx", "Document Editing", "TypeScript"]
lastModified: "2026-04-03"
---

[docx](https://www.npmjs.com/package/docx) by Dolan Miu is one of the closest JavaScript/TypeScript comparisons to safe-docx. It is an excellent open-source library for generating Word documents programmatically and for patching existing templates. safe-docx is built for a narrower problem: helping agents make repeated, high-fidelity edits to existing .docx files while leaning on the document's existing formatting patterns.

## Greenfield vs brownfield

**docx** is designed for **greenfield** document creation — generating new .docx files from scratch. It provides a declarative API for building documents programmatically: financial reports, proposals, letters, and any document where you define the structure and content from nothing. It has a patching feature for modifying existing documents, but its core strength is generation.

**safe-docx** is designed for **brownfield** editing — opening an existing .docx file and making surgical changes while preserving everything else. The typical scenario: you receive a contract from opposing counsel with specific formatting, styles, and layout, and you need to edit text without disturbing any of it. Examples of preservation-oriented tests in the public corpus include [preserving XML element structure through parse and serialize](/engineering/safe-docx/xml-parsing/parse-and-serialize-preserves-element-structure) and [setting table cell padding without losing the table property containers](/engineering/safe-docx/layout/set-table-cell-padding-creates-tc-pr-and-tc-mar-containers).

## Smart defaults vs explicit specification

This is the most important practical difference for AI agent workflows.

When docx patches an existing document, the agent needs to explicitly specify formatting for each edit — font, size, bold, italic, spacing, and so on. If the agent forgets any attribute, the edit may not match the surrounding text. Over hundreds or thousands of edits in a single session, even small omissions compound into visible formatting inconsistencies.

safe-docx uses **smart defaults based on the existing document context.** When you insert a new paragraph or replace text, safe-docx looks at the surrounding content and applies matching formatting automatically. The agent does not need to remember and specify every formatting attribute on every call. This matters most for high-volume editing — when a document is edited over a thousand times in a single session, smart defaults prevent the gradual formatting drift that explicit specification allows.

## Document comparison

safe-docx can compare two document versions and generate a tracked-changes output — the standard redline format lawyers use, with test coverage for [comparing session edits against the original](/engineering/safe-docx/compare-documents/compares-session-edits-against-original). This is not a feature docx provides. For legal workflows where lawyers edit a document and then need to produce a comparison showing what changed, safe-docx handles this natively.

## Comments and footnotes

safe-docx includes tools for adding, deleting, and updating comments and footnotes — features lawyers use frequently to indicate nuance or cite authority, with comment behavior documented in the [single-paragraph range metadata test](/engineering/safe-docx/comments/resolves-single-paragraph-range-metadata). These are not part of the docx package's core feature set.

## When to use docx

docx is the right choice when you need to:

- Generate new Word documents from scratch — reports, proposals, letters
- Use a declarative API to build document structure programmatically
- Create documents where you control every formatting decision from the start

It is a well-maintained package with a clear API and good documentation.

## When to use safe-docx

safe-docx is the right choice when you need to:

- Edit existing Word documents from AI agents with formatting preservation
- Compare document versions and generate tracked-changes output
- Add comments and footnotes to existing documents
- Make hundreds or thousands of edits without formatting drift

## They are not in conflict

If docx works for your use case, use it. safe-docx is most relevant when you are editing existing documents — contracts, agreements, regulatory filings — where preserving the original formatting and layout matters, and where document comparison is part of the workflow. If you tried docx's patching and found formatting issues on existing documents, safe-docx may solve those specific problems.
