When I run the example from using Node.js v22.1.0 (with prev.21 also):
const createReport = require('docx-templates');
const fetch = require('node-fetch');
const getFunFact = async () => {
try {
// Replace with a real API endpoint to fetch fun facts
const response = await fetch('https://api.funfacts.com');
const data = await response.json();
return data.funFact;
} catch (error) {
console.error('Error fetching fun fact', error);
return 'Oops, we could not fetch a fun fact this time.';
}
};
const generateNewsletter = async () => {
try {
const funFact = await getFunFact();
await createReport({
template: 'NewsletterTemplate.docx',
output: 'PersonalizedNewsletter.docx',
data: {
month: 'September',
firstName: 'John',
spotlightImage: 'path/to/your/image.jpg',
spotlightText: 'This month, we have been working on some exciting projects that we can’t wait to share with you!',
funFact,
signOffName: 'The XYZ Team',
},
additionalJsContext: {
img: (src) => ({ width: 6, height: 4, path: src }),
},
});
console.log('Newsletter created successfully!');
} catch (error) {
console.error('Error generating newsletter', error);
}
};
generateNewsletter();
I found the bellow error:
# node gendoc.js
node:internal/errors:541
throw error;
^
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Promise
at Object.writeFileSync (node:fs:2356:5)
at Object.<anonymous> (/Users/cohr/Projects/MACC/docx/gendoc.js:23:4)
at Module._compile (node:internal/modules/cjs/loader:1480:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1564:10)
at Module.load (node:internal/modules/cjs/loader:1287:32)
at Module._load (node:internal/modules/cjs/loader:1103:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:168:12)
at node:internal/main/run_main_module:30:49 {
code: 'ERR_INVALID_ARG_TYPE'
}
When I run the example from using Node.js v22.1.0 (with prev.21 also):
I found the bellow error: