Node.js & TypeScript

[Typescript] ์ ์šฉ Node.js ์‚ฌ์šฉํ•˜๊ธฐ

martinooo 2022. 11. 8. 12:28

๐Ÿคณ TypeScript Global ์„ค์น˜


npm install -g typescript
  • Typescript ๊ธ€๋กœ๋ฒŒ ์„ค์น˜ 

๐Ÿคณ Npm ์„ค์น˜


npm init -y
-y ๊ธฐ๋ณธ์„ค์น˜๊ฐ’

๐Ÿคณ Typescript ํ•„์š”ํ•œ ๋ชจ๋“ˆ ์„ค์น˜


npm install ts-node typescript nodemon @types/node @types/express --save-dev

๐Ÿฑ‍๐Ÿ‘“ Typescript setting & options


tsc --init
์ƒ์„ฑ์‹œ tsconfig.json ์ƒ์„ฑ๋จ
{
  "compilerOptions": {
     "lib": [
        "es5",
        "es6"
     ],
     "target": "es5",
     "module": "commonjs",
     "moduleResolution": "node",
     "outDir": "./build",
     "esModuleInterop": true,
     "emitDecoratorMetadata": true,
     "experimentalDecorators": true,
     "sourceMap": true
  }
}

ํŒŒ์ผ์•ˆ์— ์˜ต์…˜ ์„ค์ •

๐Ÿคท‍โ™‚๏ธ Typescript Options


  • ๊ธฐ๋ณธ์ ์ธ ์˜ต์…˜์„ ์ •๋ฆฌํ•ด๋ดค์Šต๋‹ˆ๋‹ค.
compilerOptions : {} /* "๋ชจ๋“ˆ ํ‚ค": ๋ชจ๋“ˆ ๊ฐ’ ์ด๋ค„์ง */
"lib": [],  /* ์ปดํŒŒ์ผ์— ํฌํ•จ๋  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ ๋ชฉ๋ก */
"module": "commonjs", /* ๋ชจ๋“ˆ์„ ์œ„ํ•œ ์ฝ”๋“œ ์ƒ์„ฑ */
"strict": true, /* ๋ชจ๋“  ์—„๊ฒฉํ•œ ํƒ€์ž…-์ฒดํ‚น ์˜ต์…˜ ํ™œ์„ฑํ™” ์—ฌ๋ถ€ */
"outFile": "./", /* ๋‹จ์ผ ํŒŒ์ผ๋กœ ํ•ฉ์ณ์„œ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. */
"outDir": "./", /* ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ๋กœ ๊ฒฐ๊ณผ ๊ตฌ์กฐ๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. */
"sourceRoot": "", /* ์†Œ์Šค ์œ„์น˜ ๋Œ€์‹  ๋””๋ฒ„๊ฑฐ๊ฐ€ ์•Œ์•„์•ผ ํ•  TypeScript ํŒŒ์ผ์ด ์œ„์น˜ํ•  ๊ณณ */
"sourceMap": true, /* '.map' ํŒŒ์ผ ์ƒ์„ฑ ์—ฌ๋ถ€. */

๐Ÿฑ‍๐Ÿ‘“ package.json


{
    ...
    "scripts": {
        "start": "nodemon --exec ts-node app.ts"
    }
}

๐Ÿฑ‍๐Ÿ‘“ app.ts ํŒŒ์ผ์—์„œ Typescript test


npm run start ์‹คํ–‰

 

Typescript ์…‹ํŒ…ํ›„ node.js ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.