본문 바로가기

ts-node : Unknown file extension ".ts" 본문

Error

ts-node : Unknown file extension ".ts"

개발자로 거듭나기 2023. 5. 18. 09:30
반응형

발생상황

  1. npm i ts-node
  2. npx ts-node [somefile].ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for [filepath]
    at new NodeError (node:internal/errors:399:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:79:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:121:38)
    at defaultLoad (node:internal/modules/esm/load:81:20)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:605:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:64:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

해결방법 01

  • tsconfig.json 에 다음과 같이 추가
"ts-node": {
  "esm": true
}

해결방법

해결방법 02

  • js로 변환시켜서 실행하기
npx tsc [filename].ts
node [filename].js
반응형
Comments