250x250
Notice
Recent Posts
Recent Comments
- Today
- Total
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- JavaScript
- mongodb
- await
- TypeScript
- atlas
- sequelize
- til
- Node.js
- findByIdAndDelete
- Express
- EC2
- AWS
- double quote
- certbot
- TailwindCSS
- mongoose
- nginx
- css
- RDS
- https
- moment
- wil
- single quote
- async
- flutter
- Find
- clipBehavior
- Nodejs
- MYSQL
- jsonwebtoken
Link
Archives
기억 휘발 방지소
[TypeScript] Node.js에서 TypeScript 실행하기 본문
728x90
반응형
✔️ TypeScript 설치
타입스크립트를 사용하기 위해서는 먼저 typescript를 설치해야한다.
npm i typescript
혹은
npm install typescript
✔️ TypeScript로 코드 작성
먼저 간단한 코드를 작성했다. 파일은 .ts로 만들었다.
// app.ts
console.log("Hello TypeScript");
'node app.ts'를 하면 실행이 된다.
위에 코드는 실행이 된다. 하지만 node가 TypeScript를 실행시킨 것이 아니라 JavaScript로 인식하고 실행을 시킨 것이다.
다음 코드도 실행해보자
let age: number;
age = 30;
console.log(age); // SyntaxError: Unexpected token ':'
위에 코드도 실행될까?
답은 실행되지 않는다!
그럼 어떻게 TypeScript로 작성한 코드를 실행시킬 수 있을까?
두 가지 방법이 있다.
- ts-node를 사용하여 TypeScript 파일 자체를 실행하기
- JavaScript로 컴파일 후 실행
✔️ ts-node
ts-node는 TypeScript 코드를 JavaScript로 컴파일하지 않고 직접 실행시키는 라이브러리이다.
'npm i ts-node' 혹은 'npm install ts-node'로 설치할 수 있다.
✔️ JavaScript로 컴파일 후 실행
typescript를 설치하면 tsc 명령어를 사용할 수 있다.
'tsc 파일명'를 입력하면 '파일명.js'파일이 같은 폴더 경로에 생성되고 생성된 파일을 실행시키면 된다.
또는 다음과 같은 방법도 있다.
tsc --init
을 실행하면 tsconfig.json 파일이 생성되고 원하는대로 설정을 수정하고 tsc를 입력하면 된다.
tsc를 입력하면 tsconfig.json에서 rootDir에 있는 경로에 있는 .ts 파일들을 컴파일하게 되고 outDir을 지정하면 .js파일이 지정한 디렉토리에 위치한다.
728x90
반응형
'Web > TypeScript' 카테고리의 다른 글
[TypeScript] 리터럴 타입 (Literal Types) (0) | 2022.02.12 |
---|---|
[TypeScript] 함수 (Function) (0) | 2022.02.11 |
[TypeScript] 인터페이스 (Interface) (0) | 2022.02.11 |
[TypeScript] 기본타입 (0) | 2022.02.11 |
[TypeScript] Express에 TypeScript 적용하기 (0) | 2022.02.08 |