docx4js is a JavaScript library designed for parsing and manipulating Microsoft Word (.docx) and PowerPoint (.pptx) files. The current stable version is 3.3.0, though the project's major releases have a less frequent cadence, with the latest stable version published two years ago. It supports both Node.js and browser environments. A key differentiator is its performance-oriented parsing strategy: it traverses document content and identifies OpenXML models using a visitor pattern, rather than building and retaining a full in-memory parsed structure. This approach aims for lower memory consumption, making it suitable for environments where memory is a concern. Users can define custom handlers to extract specific content, styles, or attributes from the document, allowing for flexible data extraction tailored to application needs. While initially focused on DOCX, it gained PPTX support in version 3.1.30. It primarily serves use cases requiring content extraction, transformation, or minor modification of Office OpenXML documents.
npm install docx4jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates loading a DOCX file from disk (Node.js), rendering its basic structure, and extracting plain text content using a custom model handler. It illustrates the core `load`, `render`, and `parse` APIs.
Thoroughly review the release notes and migration guides for each major version jump. Expect a complete rewrite of integration code when upgrading between v1, v2, and v3.
Design your application to utilize the visitor pattern (`docx.parse(handler)`) or a rendering function (`docx.render(createElement)`) to process content. Do not expect to modify a traditional document object model after initial parsing. Modifications typically involve creating a new document or using specific save features.
Verify that your specific Office OpenXML file type (.docx or .pptx) is supported by your docx4js version. Do not assume support for Excel (.xlsx) files.
For browser usage, ensure that file content is provided as a `Blob` or `ArrayBuffer` obtained through client-side file APIs (e.g., `FileReader`). Avoid Node.js-specific `fs` imports.
Ensure your build process (Webpack, Rollup, etc.) correctly shims or excludes Node.js modules for browser builds. When `docx4js.load()` is called, pass a `Blob` or `ArrayBuffer` in the browser instead of a file path.
Validate the input file (ensure it exists, is a valid .docx/.pptx, and is not empty) before passing it to `docx4js.load()`. Check the file buffer or blob content for integrity.
Ensure your runtime environment provides the `URL` global object. In Node.js, this usually means using a recent version. For browser environments, ensure your build setup includes necessary polyfills if targeting older browsers.