-
[TypeScript] Error : exports is not defined (import 오류)공부/나를 힘들게하는 오류들 2024. 3. 26. 23:51
원인
코딩애플 강의를 들으면서 import를 시도하는 예제였는데 Live html 화면에서 오류가 발생했다.
찾아보니 브라우저는 commonjs 모듈을 지원하지 않아 발생하는 오류라고 한다.
해결방법
설정 tsconfig.json 파일에 module 옵션을 제거 또는 "ES2015" , "ESNext"로 설정하면
import 및 export문이 원본 파일에 있는 그대로 유지된다고 한다.추가로 html 파일의 script 태그에 유형 속성을 설정한다.
//내 사례에서는 index.js <script type="module" src="index.js"></script>
이렇게 설정했더니 오류가 해결됐다.!!
참고한 글 :
TypeScript : Uncaught ReferenceError: exports is not defined
문제 정의 : 코딩애플 Ts 강의 import/export 파트 따라 하는데 발생. 내 기억으로는 내가 뭐 따로 세팅한거 없고 pure 환경이었던 걸로 기억한다. type만 따로 빼서 기존의 컴포넌트에 import 하려 했는데
dev-minju.tistory.com
How to fix "ReferenceError: exports is not defined" in a pure TypeScript project?
If I write a .ts with any kind of import or export instruction the resulting .js will generate the following error when loaded in an HTML page: "ReferenceError: exports is not defined" H...
stackoverflow.com