npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@niicojs/word

v0.3.0

Published

Downloads

86

Readme

@niicojs/word

A lightweight TypeScript library for Word document (.docx) manipulation. Merge documents, manage pages, and more.

Features

  • Document Merging - Combine multiple DOCX files into one
  • Page Management - Add page breaks, remove pages, get page information
  • Selective Merging - Choose specific pages to merge from source documents
  • Templating - Replace placeholders like {name} in body, tables, headers, and footers
  • Full Structure Preservation - Maintains styles, fonts, headers, footers, and other document properties
  • Zero Native Dependencies - Pure JavaScript/TypeScript implementation

Installation

npm install @niicojs/word
# or
bun add @niicojs/word
# or
pnpm add @niicojs/word

Quick Start

Merge Multiple Documents

import { Document } from '@niicojs/word';

// Create an empty output document
const output = Document.create();

// Merge documents (page breaks are automatically added between documents)
await output.merge('document1.docx');
await output.merge('document2.docx');
await output.merge('document3.docx');

// Save the merged document
await output.toFile('merged.docx');

Load and Inspect a Document

import { Document } from '@niicojs/word';

const doc = await Document.fromFile('document.docx');

console.log(`Page count: ${doc.getPageCount()}`);
console.log(`Pages:`, doc.getPages());

Merge Specific Pages

import { Document } from '@niicojs/word';

const output = Document.create();

// Merge only pages 0 and 2 (first and third pages)
await output.merge('source.docx', { pages: [0, 2] });

// Merge without adding a page break before
await output.merge('another.docx', { addPageBreakBefore: false });

await output.toFile('output.docx');

Work with Buffers

import { Document } from '@niicojs/word';

// Load from buffer
const data = await fetch('https://example.com/document.docx')
  .then((res) => res.arrayBuffer())
  .then((buf) => new Uint8Array(buf));

const doc = await Document.fromBuffer(data);

// Save to buffer
const outputBuffer = await doc.toBuffer();

Page Management

import { Document } from '@niicojs/word';

const doc = await Document.fromFile('document.docx');

// Add a page break at the end
doc.addPageBreak();

// Remove a specific page (0-based index)
doc.removePage(1); // Removes the second page

await doc.toFile('modified.docx');

Templating

import { Document } from '@niicojs/word';

const doc = await Document.fromFile('template.docx');

doc.render({
  name: 'Ava',
  city: 'Paris',
});

await doc.toFile('filled.docx');

API Reference

Document Class

Static Methods

| Method | Description | | --------------------------- | --------------------------------- | | Document.create() | Create a new empty document | | Document.fromFile(path) | Load a document from a file path | | Document.fromBuffer(data) | Load a document from a Uint8Array |

Instance Methods

| Method | Description | | ------------------------- | ----------------------------------- | | toFile(path) | Save the document to a file | | toBuffer() | Save the document to a Uint8Array | | getPageCount() | Get the number of pages | | getPages() | Get information about all pages | | addPageBreak() | Add a page break at the end | | removePage(index) | Remove a page by index (0-based) | | merge(source, options?) | Merge content from another document | | render(data, options?) | Replace template placeholders |

MergeOptions

interface MergeOptions {
  /** Page indices to include (0-based). If omitted, all pages are merged. */
  pages?: number[];

  /** Insert page break before merged content. Default: true */
  addPageBreakBefore?: boolean;
}

TemplateOptions

type TemplateValue = string | number | boolean | null | undefined;

type TemplateData = Record<string, TemplateValue>;

interface TemplateOptions {
  /** Placeholder pattern. Must contain one capture group for the key. */
  pattern?: RegExp;

  /** Remove placeholders when the key is missing. Default: false */
  removeMissing?: boolean;

  /** Optional value transformer applied before insertion. */
  transform?: (value: TemplateValue, key: string) => string;
}

PageInfo

interface PageInfo {
  /** 0-based page index */
  index: number;

  /** Index of first body element in this page */
  startElement: number;

  /** Index of last body element in this page (inclusive) */
  endElement: number;
}

How It Works

This library works by:

  1. Reading DOCX files as ZIP archives
  2. Parsing the XML content (specifically word/document.xml)
  3. Manipulating the document structure in memory
  4. Writing back to a valid DOCX format

Page detection is based on explicit page break elements (<w:br w:type="page"/>). Note that Word may render page breaks in different positions based on content flow, fonts, and page size - this library only detects explicit page breaks.

When merging documents, the library:

  • Preserves all namespace declarations from source documents
  • Copies document structure (styles, fonts, headers, footers, etc.) from the first source
  • Combines body content with configurable page breaks between documents

Requirements

  • Node.js >= 20
  • Works with Bun and other modern JavaScript runtimes

Dependencies

License

MIT