TypeScript를 사용하면 코드를 작성하고 확인하고 싶으면 컴파일을 진행한 뒤에 코드를 실행할 수 있었다.
이는 개발 속도를 저하시키게 된다.
ts-node
를 사용하면 TypeScript 파일(.ts
또는 .tsx
)을 직접 실행할 수 있다.
TypeScript 코드를 자동으로 컴파일하여 JavaScript로 변환한 후 Ndoe.js에서 실행시켜준다.
npm init -y
npm install -D typescript ts-node
tsconfig.json
{
"compilerOptions": {
"module": "CommonJS",
"outDir": "./dist",
"target": "ES6",
"esModuleInterop": true,
"strict": true,
"baseUrl": "./src",
"paths": {
"@user/*": ["user/*"]
}
},
"include": ["src/**/*"],
"exclude": ["**/*.test.ts"]
}
src/message.ts
const message: string = "hello world"
console.log(message)
package.json
"scripts": {
"dev": "ts-node --project tsconfig.json ./src/message"
},
npm run dev
@경로를 사용하려면 tsconfig-paths
모듈을 설치해야 한다.
npm install -D tsconfig-paths
user/service/user.service.ts
const variable: string = "user"
export default variable
"scripts": {
"dev": "ts-node -r tsconfig-paths/register --project tsconfig.json ./src/message"
},
npm run dev
Node.js 소스 코드의 변경을 감지해서 자동으로 서버를 재시작해주는 모듈이다.
npm install -D nodemon
nodemon.json
{
"watch": ["src/**/*"],
"ext": "ts",
"exec": "ts-node -r tsconfig-paths/register ./src/message"
}
"scripts": {
"dev": "nodemon"
}
npm run dev
파일을 수정하면 nodemon에 의해서 서버가 재시작 되고 ts-node에 의해 컴파일이 자동으로 진행되게 된다.