Files
docx/docs/quickstart.md
DolanandClaude Opus 5.5 9239334f73 docs: pipe Packer.toStream's stream straight away in the quickstart (#3835)
The quickstart called `.then` on `Packer.toStream`'s result, but it returns the stream itself, not a promise, so the
example threw "then is not a function". The Packers page already uses the stream directly.


Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 22:28:42 +01:00

5.5 KiB

Quickstart Guide

Get up and running with docx in under 5 minutes.

In this guide:

  • Install docx
  • Create your first document
  • Add text, tables, and images
  • Export in multiple formats

Installation

npm install --save docx

In Node, docx needs Node.js 16 or later. docx/html and docx/embedded-files need 16.9 or later, and docx/layout 18 or later.

Your First Document

Create a "Hello World" document in just a few lines:

import { Document, Packer, Paragraph, TextRun } from "docx";
import * as fs from "fs";

const doc = new Document({
    sections: [
        {
            // Documents are organized into sections
            children: [
                new Paragraph({
                    children: [new TextRun("Hello World!")],
                }),
            ],
        },
    ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});

That's it! Run this code to create your first Word document.

Adding Common Elements

Bold and Styled Text

import { Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun("Normal text, "),
                        new TextRun({
                            text: "bold text, ",
                            bold: true,
                        }),
                        new TextRun({
                            text: "italic text",
                            italics: true,
                        }),
                    ],
                }),
            ],
        },
    ],
});

See the Text guide for all formatting options.

Headings

import { Document, HeadingLevel, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    text: "My Heading",
                    heading: HeadingLevel.HEADING_1,
                }),
                new Paragraph("Some text under the heading."),
            ],
        },
    ],
});

Bullet Points

import { Document, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    text: "First item",
                    bullet: { level: 0 },
                }),
                new Paragraph({
                    text: "Second item",
                    bullet: { level: 0 },
                }),
                new Paragraph({
                    text: "Nested item",
                    bullet: { level: 1 },
                }),
            ],
        },
    ],
});

See the Bullet Points guide for more options.

Tables

import { Document, Paragraph, Table, TableCell, TableRow } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Table({
                    rows: [
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("Cell 1")] }),
                                new TableCell({ children: [new Paragraph("Cell 2")] }),
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

See the Tables guide for advanced table features.

Images

import { Document, ImageRun, Paragraph } from "docx";
import * as fs from "fs";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ImageRun({
                            type: "png",
                            data: fs.readFileSync("./demo/assets/images/dog.png"),
                            transformation: {
                                width: 200,
                                height: 200,
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});

See the Images guide for positioning and wrapping options.

Export Options

Node.js - Save to File

import * as fs from "fs";

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("document.docx", buffer);
});

Node.js - Stream

import * as fs from "fs";

Packer.toStream(doc).pipe(fs.createWriteStream("document.docx"));

Browser - Download

import { saveAs } from "file-saver";

Packer.toBlob(doc).then((blob) => {
    saveAs(blob, "document.docx");
});

Base64 String

Packer.toBase64String(doc).then((base64) => {
    console.log(base64);
});

See the Packers guide for all export options.

Next Steps

Now that you've created your first document, explore these guides:

?> Check out the demo folder for 90+ working examples covering every feature.