Nestjs readFileSync return Cannot read property 'readFileSync' of undefined

Viewed 5525

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

3 Answers

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

Try

import {readFileSync} from 'fs'

I fix it by change:

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

to:

import fs = require('fs');
import path = require('path');
Related