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

docx-templer

v1.0.0

Published

A library for merging and embedding multiple DOCX or word documents to single output document

Readme

docx-templer

A high-performance Node.js library for merging and embedding DOCX documents using simple placeholders. Designed to be fast, reliable, and safe against common Word XML pitfalls.


✨ Features

  • Merge one DOCX into another using clean placeholders
  • Embed multiple documents into a single template
  • Works reliably with Word-generated XML (handles split runs)
  • Buffer-based API (perfect for APIs, servers, and cloud environments)
  • No Word automation or OS-level dependencies

📦 Installation

npm install docx-templer

For TypeScript projects:

npm install --save-dev @types/docx-templer

🚀 Usage

Merger (Recommended)

Merges a single source DOCX into a base template. This is the most portable method and works across platforms such as Microsoft Word, Google Docs, and online viewers.

const { Merger } = require('docx-templer');
const fs = require('fs');

const templateBuffer = fs.readFileSync('template.docx');
const sourceBuffer = fs.readFileSync('source.docx');

Merger(templateBuffer, sourceBuffer, 'MY_PLACEHOLDER')
  .then(mergedBuffer => {
    fs.writeFileSync('output.docx', mergedBuffer);
  });

Embedder

Embeds multiple DOCX documents into a single template using multiple placeholders.

⚠️ Note: This method relies on Word-specific embedding behavior. It works best in Microsoft Word and may not be supported by platforms like Google Docs.

const { Embedder } = require('docx-templer');
const fs = require('fs');

const templateBuffer = fs.readFileSync('template.docx');
const sourceBuffers = [
  { buffer: fs.readFileSync('source1.docx'), placeholder: 'key1' },
  { buffer: fs.readFileSync('source2.docx'), placeholder: 'key2' }
];

Embedder(templateBuffer, sourceBuffers)
  .then(embeddedBuffer => {
    fs.writeFileSync('output.docx', embeddedBuffer);
  });

📝 How to Place Tags in Word

To ensure successful merging, follow these rules when preparing your template.docx:


1. Tag Pattern

Always use three underscores before and after your key.

Correct:

___YOURPLACEHOLDER___

Incorrect:

{YOURPLACEHOLDER}
YOURPLACEHOLDER

2. Best Practices

  • Type tags cleanly
    Avoid backspacing while typing the tag. If you make a mistake, delete the whole line and re-type it.
    This prevents Word from creating extra XML nodes.

  • Stand-alone paragraphs
    For the best layout, place the tag on its own line.
    The library replaces the entire paragraph containing the tag with the content of the source document.


📚 API Reference

Merger(templateBuffer, sourceBuffer, placeholder)

Merges a single source document into the template at the specified placeholder.

  • templateBuffer: Buffer
    The main template document.

  • sourceBuffer: Buffer
    The document to be inserted.

  • placeholder: string
    The key inside the underscores (e.g., passing 'DATA' looks for ___DATA___).

  • Returns: Promise<Buffer>


Embedder(templateBuffer, sourceBuffers)

Embeds multiple source documents into the template using multiple placeholders.

  • templateBuffer: Buffer
    The main template document.

  • sourceBuffers: Array<{ buffer: Buffer, placeholder: string }>
    An array of objects containing the source document buffer and its unique placeholder key.

  • Returns: Promise<Buffer>


✅ Recommendations

  • Use Merger wherever possible for maximum compatibility
  • Keep placeholders isolated in their own paragraph
  • Avoid styling or editing placeholders after typing them
  • Maintain basic formatting and standard fonts in templates and source documents
  • Avoid nested formatting, complex styles, deeply nested tables, or excessive section breaks when using Merger
  • If the Merger output shows a warning while opening the document in Word, try using Embedder instead for a better viewing and editing experience
  • For complex documents (heavy formatting, multiple tables, headers/footers, or mixed content), prefer the Embedder method as it is more resilient and helps avoid Word XML corruption or broken-document errors

📄 License

MIT