node.jsnestjsreadfile

Nestjs readFileSync return Cannot read property 'readFileSync' of undefined


I trying get file using method readFileSync:

import fs from 'fs';
import path from 'path';

const templateFile = fs.readFileSync(
    path.resolve(
    __dirname,
    '../mail/templates/exampleTemplate.html',
    ),
    'utf-8',
);

Nest still return me error:

TypeError: Cannot read property 'readFileSync' of undefined

I tryied used to path: ./templates/exampleTemplate.html, but result is the same

I have a structure file:

enter image description here


Solution

  • Typescript does not have a default export of fs, which is could simply solved by aliasing the whole import.

    import * as fs from 'fs'