Error reading a file with FS


#1

Hi All,

I’m trying to take form data and put it into a Word doc template (*.docx). To use the DocxTemplater library, I need File System access.

I followed Mike’s post to import FS and @types/node, but I’m still getting this problem…

Errors

Uncaught (in promise): TypeError: fs.readFileSync is not a function (from tests 1 & 2 below)

Uncaught (in promise): TypeError: Object(...) is not a function (from test 3 below)

NPM Installs

npm install docxtemplater --save
npm install jszip@2 --save
npm install @types/node --save

Code

import { readFileSync } from 'fs';

declare var require: any;

var JSZip = require('jszip');
var Docxtemplater = require('docxtemplater');
var fs = require('fs');
var path = require('path');
ReadTest1() {
    let filename = "./home.html";
    let data = fs.readFileSync(filename, 'utf8');
    console.log(data);
}

ReadTest2 {
    let filename = "./home.html";
    fs.readFile(filename, "utf8", function(err, data) {
        if (err) {
            console.log('error: ' + err.toString());
        } else {
            console.log(data.toString());
        }
    });
}

ReadTest3() {
    let filename = "./home.html";
    let data = readFileSync(filename, 'utf8');
    console.log(data);
}

I have fs listed twice there to try a few different ways to gain access to the fs read* functions.

DocxTemplater’s simple Node example:
https://docxtemplater.readthedocs.io/en/latest/generate.html

Any help is appreciated!

Thanks,
Ryan