타입스크립트(TypeScript) 컴파일러(Compiler)와 ts-node 설치 및 실행 방법

2021. 10. 25. 15:26Blockchain/Project - Coin Swap

컴파일러

* TS 코드를 컴파일하여 JS 코드로 변환 후 JS 파일을 실행

 

- 설치

  1. npm install -g typescript // 설치(글로벌)
  2. tsc -v // 설치 완료 및 버젼 확인

 

- 실행

  1. test.ts 파일 생성 후 코드 작성
    function add(numb1:number, numb2:number) {
        console.log(numb1 + numb2);
    }
    
    add1(1, 2); // 3
  2. tsc test.ts // 컴파일
  3. test.js 파일 생성 확인
    컴파일 완료
  4. node test.js // 컴파일 된 js 파일 실행
    실행 결과 확인

ts-node

* TS 코드의 JS로 변환과 실행을 동시에 하려면 ts-node 설치가 필요

 

- 설치

  1. npm install -g ts-node // 설치(글로벌)
    npm install --save-dev @types/node
  2. ts-node -v // 설치 완료 및 버젼 확인

 

- 실행

  1. test2.ts 파일 생성 후 코드 작성
    function showItems(arr:number[]) {
        arr.forEach((item) => {
            console.log(item);
        });
    }
    
    showItems([1, 2, 3]);​
  2. ts-node hello.ts // 컴파일과 실행 동시 진행
    실행 결과 확인