본문 바로가기

개발환경관련

[NodeJS]TypeScript 컴파일러 설치

TypeScript 컴파일러는 NodeJS 환경에서 실행된다. Node가 설치되지 않았다면 먼저 설치를 하여야 한다.

Node가 설치되어 있으면 npm(node package manager)를 이용하여 TypeScript 컴파일러르 설치한다.

 

1. 터미널 또는 명령 프롬프트에서 다음 명령어를 실행한다.  (-g 옵션은 typescript를 전역으로 설치하기 위한 것)

 

$npm install -g typescript

 

 

 

 

2. TypeScript Compiler 설치 확인 

(1) 간접적으로 TypeScript가 제대로 설치되었는지를 확인 방법 - 다음 명령어를 통해 설치된 TypeScript Compiler 버전을 확인한다.

TypeScript Compiler가 설치되었디면 다음 명령어를 실행하였을 때 설치된 컴파일러의 버전이 출력된다. 

 

$tsc --version

 

(2) npm 명령어를 이용하여 설치된 package를 확인한다. 

$npm -g list

패키지 트리에서 설치된 TypeScript와 버전 정보가 typescript@5.7.3 형태로 표시된다. 패키지 트리에 typescript가 확인되면 TypeScript Compiler가 설치된 것이다. 

 

3. TypeScript Source Code 작성(파일명: hello.ts)하고 컴파일하기 

// hello.ts
function greet(name: string): string {
  return 'Hello, ${name}!';
}

const userName: string = "Alice";
console.log(greet(userName));

 

4. 작성한 hello.ts를 다음과 같이 컴파일하면 hello.js 의 JavaScript Source Code가 생성된다. 

$tsc hello.ts

 

5. 컴파일하여 다음의 JavaScript Source Code가 생성된다. 

// hello.ts
function greet(name) {
    return "Hello, ".concat(name, "!");
}
var userName = "Alice";
console.log(greet(userName));

 

6. node를 이용해서 다음과 같이 hello.js를 실행시키면 Hello, Allice! 가 출력된다. 

$node hello.js
Hello. Alice!