Home > Software design >  TypeError: TemplateHandler is not a constructor nodejs
TypeError: TemplateHandler is not a constructor nodejs

Time:08-11

I am trying to use a node library "easy-template-x" (https://www.npmjs.com/package/easy-template-x#node-example) to convert word template to docx file by passing JSON data. While running i am getthing this error

const handler = new TemplateHandler();
                ^
TypeError: TemplateHandler is not a constructor

Any idea where i am doing mistake

const TemplateHandler = require('easy-template-x')
const fs = require('fs')

const http = require('node:http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello, World!\n');
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

const templateFile = fs.readFileSync('./template.docx');

const data = {
    posts: [
        { author: 'Alon Bar', text: 'Very important\ntext here!' },
        { author: 'Alon Bar', text: 'Forgot to mention that...' }
    ]
};

const handler = new TemplateHandler();
start();
const start = async function getNum() {
    const doc = await handler.process(templateFile, data);

    // 3. save output
    fs.writeFileSync('./myTemplate - output.docx', doc);
}

CodePudding user response:

import it like that (named import):

import { TemplateHandler } from 'easy-template-x';

as it's documented here

https://www.npmjs.com/package/easy-template-x

  • Related