TypeScript : ts 파일을 바로 실행하는 법 ts-node 사용법

JooSehyun·2025년 5월 5일
0

[Study]

목록 보기
59/59
post-thumbnail

🕵️ 프로젝트에서 index.ts을 만들고 터미널에서 테스트를 해보고싶은데 js파일로 컴파일하고 나서 node로 다시 실행하는게 번거로워 한방에 실행시키는 방법을 알게되어 쓰게 되었다.

현재

1️⃣npx tsc
2️⃣node index.js


현재 폴더 구조

📂folder
├── index.ts
├── package.json
├── package-lock.json
└── node_modules

먼저 tsconfig 파일 생성

명령어

npx tsc --init

🗂️ tsconfig.ts

{
  "compilerOptions": {
    "target": "ES2015",              
    "module": "commonjs",            
    "lib": ["ES2015", "DOM"],        
    "strict": true,
    "esModuleInterop": true,
    "moduleResolution": "node",
    "outDir": "./dist",
    "rootDir": "./",
    "skipLibCheck": true      
  },
  "include": ["**/*.ts"]
}

여기서 outDir에서 js컴파일 파일이 생성된다.

"outDir": "./dist",
"rootDir": "./",

여기서 매번 index.ts에서 코드를 추가하고 node로 실행하려면 매번 컴파일을 하고 실행을 해야한다.

ts-node 모듈

[NPM] ts-node 링크


🕵️ ts-node란?

ts-node는 TypeScript 파일(.ts)을 컴파일 없이 바로 실행할 수 있게 해주는 Node.js 실행기입니다.

기능설명
즉시 실행.ts 파일을 따로 tsc로 컴파일하지 않고 바로 실행
빠른 개발테스트할 때 빠르고 간편함 (console.log() 확인 등)
설정 연동tsconfig.json 설정도 적용됨

설치 명령어
npm i -g ts-node

실행 방법
ts-node index.ts


0개의 댓글