docx 7: does not work example of basic usage when using ES modules format in Node.js
See original GitHub issueExample 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:
- Created 2 years ago
- Comments:21 (17 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can import like this:
You still get the nice hinting too