๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

201124

ยท ์•ฝ 2๋ถ„

ts-node ์ ˆ๋Œ€๊ฒฝ๋กœ#

ts-node ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

yarn add -D typescript @types/node nodemon ts-node tsconfig-paths

package.json์—์„œ nodemon์œผ๋กœ ์‹คํ–‰

"scripts": {  "dev": "nodemon -r ts-node src/index.ts" // nodemon src/index.ts๋กœ๋„ ์‹คํ–‰๋จ}

tsconfig.json์— baseUrl๊ณผ path ์„ค์ •

"baseUrl": ".","paths": {  "src/*": ["src/*"]}

โ†’ ์ด์ œ ์ปดํŒŒ์ผ์€ ๋˜์ง€๋งŒ, ์ปดํŒŒ์ผ๋œ ํŒŒ์ผ์„ node, ts-node๊ฐ€ ์ธ์‹ํ•˜์ง€ ๋ชปํ•œ๋‹ค.

โ†’ tsconfig-paths ๋ชจ๋“ˆ๋กœ ๊ฐ€๋Šฅ

"scripts": {  "dev": "nodemon --exec ts-node -r tsconfig-paths/register src/index.ts"}

๋นŒ๋“œ ํ›„์—๋„ ๋™์ž‘ํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด

  1. ts-node ๋ชจ๋“ˆ์„ ํ†ตํ•ด ์ž‘๋™์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

    "scripts": {    "start": "ts-node -r tsconfig-paths/register dist/index.js"    // ๋˜๋Š”    // "start": "node -r ts-node/register/transpile-only -r tsconfig-paths/register dist/index.js"}
  2. tsconfig-paths์˜ bootstrap์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•.

    // tsconfig-paths-bootstrap.jsconst tsConfigPathFile = require('./tsconfig.paths.json');const tsConfig = require('./tsconfig.json');const tsConfigPaths = require('tsconfig-paths');
    tsConfigPaths.register({    baseUrl: tsConfig.compilerOptions.outDir,    paths: tsConfigPathFile.compilerOptions.paths,});
    // package.json"scripts" {  "start": "node -r ./tsconfig-paths-bootstrap.js dist/src/index.js"}

์ฐธ๊ณ ์ž๋ฃŒ#

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ ์ ˆ๋Œ€๊ฒฝ๋กœ ์“ฐ๊ธฐ

typescript + node absolute path