10

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

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
michal
  • 1,534
  • 5
  • 28
  • 62

3 Answers3

32

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

Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44
  • How is NextJS able to allow typescript files importing fs the normal way such as `import fs from 'fs'`?, is it through tsconfig.json? – Caleb Taylor Jul 31 '22 at 04:58
12

Try

import {readFileSync} from 'fs'
Kai Sheng Tung
  • 400
  • 1
  • 10
0

I fix it by change:

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

to:

import fs = require('fs');
import path = require('path');
michal
  • 1,534
  • 5
  • 28
  • 62