Files
docx/docs/quickstart.md
T
Dolan 3539c06e92 docs: Improve inline docs and docs website (#3353)
* docs: Improve inline docs and docs website

* Fix lint

* Fix spelling
2026-02-14 03:54:25 +00:00

3.8 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

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

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 { HeadingLevel } from "docx";

new Paragraph({
    text: "My Heading",
    heading: HeadingLevel.HEADING_1,
});

Bullet Points

[
    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 { Table, TableRow, TableCell } from "docx";

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 { ImageRun } from "docx";

new Paragraph({
    children: [
        new ImageRun({
            type: "png",
            data: fs.readFileSync("./image.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).then((stream) => {
    stream.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.