본문 바로가기
Framework | Test/Nest.js

[Resolved Error] TypeError: Cannot read property 'readFileSync' of undefined

by Baest 2022. 7. 26.

업무 중 해결 했던 에러에 대하여, 기록한다.

 

Nest.js 프로젝트에서 fs 의 readFileSync 를 사용하여 파일을 읽으려고 했는데, 아래와 같은 에러 메세지가 발생했다.

 

TypeError: Cannot read property 'readFileSync' of undefined

 

stackoverflow의 답변을 통해 해결했는데, 아래와 같이 import * as 를 해주면 readFileSync 를 사용할 수 있다.

기존에 우리는 import { } from 'fs' 이런식으로 했었던 것 같다. 

포스팅을 해두려고 에러 메세지를 스크린샷으로 남겨뒀었는데, 업무 중의 트러블슈팅이라 스크린샷은 PASS...

 

추후 참고할 수 있도록 스택오버플로우 링크도 남겨둔다 :)

 

import * as fs from 'fs'

 

https://stackoverflow.com/questions/66214269/nestjs-readfilesync-return-cannot-read-property-readfilesync-of-undefined

 

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/exampleTemp...

stackoverflow.com