docx: docx 7: does not work example of basic usage when using ES modules format in Node.js
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)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 28 (22 by maintainers)
You can import like this: