Getting Started with JavaScript Word Library
19 Oct 20263 minutes to read
The JavaScript Word Library facilitates the creation of Word documents with basic elements from scratch.
This guide explains how to integrate the JavaScript Word Library into a React application that runs in the browser. The generated Word document is downloaded directly from the browser; no server-side document processing is required.
Prerequisites
Before you begin, make sure you have the following installed:
- Node.js 18 or later.
- npm 9 or later, or Yarn 1.22 or later.
- React 18 or later.
- Visual Studio Code, Code Studio, or another code editor.
- A supported browser such as the latest versions of Microsoft Edge, Google Chrome, or Mozilla Firefox.
To verify your Node.js and npm versions, run:
node --version
npm --versionCreate a React Project
This guide uses Vite to scaffold the React project. Run the following command in a terminal:
npm create vite@latest my-docx-app -- --template react
cd my-docx-appAfter the project is created, install its dependencies:
npm installInstall the JavaScript Word Library
All Syncfusion® JS 2 packages are published in the npmjs.com registry. The npm install command below resolves @syncfusion/ej2-docx to the latest stable version compatible with React 18 or later.
- To install the
JavaScript Word Library, use the following command.
npm install @syncfusion/ej2-docx --saveCreate a Word Document
Replace the contents of App.jsx with the following code. The file imports the required classes from @syncfusion/ej2-docx and creates a Word document.
import React from 'react';
import {WordDocument} from '@syncfusion/ej2-docx';
export default function App() {
let createDocument = async () => {
// Creates a Word document.
let document = WordDocument.create();
let section = document.sections[0];
// Get paragraph from section
let paragraph = section.body.paragraphs[0];
// Add text run and format it
let run = paragraph.appendTextRange();
run.text = 'Hello World!!';
run.characterFormat.bold = true;
run.characterFormat.fontSize = 24;
// Saves and downloads the document.
await document.save('Output.docx');
};
return (
<div style={{ padding: '1.5rem' }}>
<button onClick={createDocument}>
Create Word document
</button>
</div>
);
}Note: This sample uses named imports from the npm package (
import { WordDocument } from '@syncfusion/ej2-docx'). The npm package does not expose a globalejnamespace; usingej.docx.WordDocumentwithout an import will throwReferenceError: ej is not definedin a Vite or Create-React-App build.
Run the Application
Open a terminal in the project root and start the Vite development server:
npm run devVite serves the application at http://localhost:5173. Open this URL in a browser and click Create Word document to download the generated file as Output.docx.
The generated Word document contains a single paragraph with the text “Hello World!!!”.
Note: If you used Create-React-App instead of Vite, the run command is
npm startand the default URL ishttp://localhost:3000.