본문 바로가기
Errors

[Typescript] cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

by egas 2021. 5. 21.

타입스크립트 1일차 다음과 같은 오류를 만났다. 코드는 한줄밖에없는데 무엇이 문제일까..?

 

isolatedModules err

src/js/index.ts:1:1 - error TS1208: 'index.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

 

const i = 'holee';

console.log(i);

 

> javascript-calculator@1.0.0 check-types
> tsc

src/js/index.ts:1:1 - error TS1208: 'index.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

1 const i = 'holee';
  ~~~~~


Found 1 error.

 

Typescript는 기본적으로 import/exports가 없는 파일을 레거시 취급을 한다. 타입스크립트가 이러한 파일들은 모듈이 아니기 때문에 전역 네임스페이스에 병합한다.

 

tsconfig.json에서 isolatedModules 옵션을 false하거나 export {} 로 아무것도 가져오지 않고 파일을 모듈로 만들어서 해결 할 수 있다.

 

const i = 'holee';

console.log(i);

export {}

 

> javascript-calculator@1.0.0 check-types
> tsc

 

도움이 되었다면 아래 광고 한번만 눌러주세요! 큰 도움이 됩니다 :)

728x90

댓글