question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

docx 7: does not work example of basic usage when using ES modules format in Node.js

See original GitHub issue

Example of Basic Usage does not work on docx v7 (works on v6.0.3)

import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "docx";

// Documents contain sections, you can have multiple sections per document, go here to learn more about sections
// This simple example will only contain one section
const doc = new Document({
    sections: [{
        properties: {},
        children: [
            new Paragraph({
                children: [
                    new TextRun("Hello World"),
                    new TextRun({
                        text: "Foo Bar",
                        bold: true,
                    }),
                    new TextRun({
                        text: "\tGithub is the best",
                        bold: true,
                    }),
                ],
            }),
        ],
    }],
});

// Used to export the file into a .docx file
Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});

// Done! A file called 'My Document.docx' will be in your file system.

import { Document, Packer, Paragraph, TextRun } from ‘docx’; ^^^^^^ SyntaxError: Named export ‘Packer’ not found. The requested module ‘docx’ is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using:

import pkg from ‘docx’; const { Document, Packer, Paragraph, TextRun } = pkg;

at ModuleJob._instantiate (internal/modules/esm/module_job.js:124:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:179:5)
at async Loader.import (internal/modules/esm/loader.js:178:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:21 (17 by maintainers)

github_iconTop GitHub Comments

2reactions
pablohirafujicommented, Jul 5, 2022

You can import like this:

import docx from "docx";
const {Document, Packer, Paragraph, TextRun} = docx;
0reactions
khill-fbmccommented, Jun 30, 2022

You still get the nice hinting too image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using ES modules in Node.js - LogRocket Blog
With ES modules, modules are defined with the use of the import and export keywords instead of the require() function in CommonJS.
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and export...
Read more >
Could not find a declaration file for module 'module-name ...
json I wrote "main": "dist/index.js" . In Node.js everything works fine, but TypeScript: import {Injector} from ...
Read more >
Html module - Docxtemplater
Your docx should contain the text: {~html} . After installing the module, you can use a working demo by running node sample.js ....
Read more >
file-loader - webpack - JS.ORG
This will emit file.png as a file in the output directory (with the ... By default, file-loader generates JS modules that use the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found