Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 940
michal Avatar asked Dec 13 '22 07:12

michal


2 Answers

Since you're using Typescript and fs does not have a default export, you have to use import * as fs from 'fs'.

like image 188
Arun Kumar Mohan Avatar answered Dec 31 '22 10:12

Arun Kumar Mohan


Try

import {readFileSync} from 'fs'
like image 23
Kai Sheng Tung Avatar answered Dec 31 '22 08:12

Kai Sheng Tung