• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

똑같이 따라하고 오류나서 다시해보고 다시해보고 올려주신 코드 확인해봐도 계속 에러가 뜨는대요 ㅠ.ㅠ

24.02.19 02:11 작성 조회수 152

0

PS C:\Users\ohdaesung\Desktop\study\fastify> npm start

> sample@1.0.0 start

> npm run build:live

> sample@1.0.0 build:live

> nodemon --watch 'src/**/*' --exec ts-node --esm src/main.ts --verbose

[nodemon] 3.0.3

[nodemon] to restart at any time, enter rs

[nodemon] or send SIGHUP to 12892 to restart

[nodemon] watching path(s): 'src\**\*'

[nodemon] watching extensions: ts,json

[nodemon] starting ts-node --esm src/main.ts

[nodemon] spawning

[nodemon] child pid: 17216

[nodemon] watching 4 files

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\Users\ohdaesung\Desktop\study\fastify\src\main.ts

at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)

at defaultGetFormat (node:internal/modules/esm/get_format:203:36)

at defaultLoad (node:internal/modules/esm/load:143:22)

at async nextLoad (node:internal/modules/esm/hooks:750:22)

at async nextLoad (node:internal/modules/esm/hooks:750:22)

at async Hooks.load (node:internal/modules/esm/hooks:383:20)

at async handleMessage (node:internal/modules/esm/worker:199:18) {

code: 'ERR_UNKNOWN_FILE_EXTENSION'

}

[nodemon] app crashed - waiting for file changes before starting... ㅠ.ㅠ

답변 1

답변을 작성해보세요.

0

일단 해당 내용은 ts파일 즉 typescript 파일이 정상적으로 인식되지 않아서 나는 오류 같습니다.

일단 몇가지 확인을 좀 부탁드리겠습니다.

 

  1. 우선 첫번째로 다음과 같이 typescript가 gloabal 옵션으로 typescript를 설치했는지 확인 부탁드립니다.

npm i typescript -g

  1. 그리고 지금 세팅한 package.json과 tsconfig.json을 첨부 부탁드리겠습니다.

  2. 또 어떤 챕터까지 완료 후에 이런 오류가 나는지도 확인 부탁드리겠습니다.

해당사항들을 확인부탁드리겠습니다.

 

 

타입스크립트 설치했습니다 ㅠ.ㅠ

다 작성후에 npm start 작동하면 오류가 납니다.

 

{
  "name": "sample",
  "version": "1.0.0",
  "description": "",
  "main": "main.ts",
  "type":"module",
  "scripts": {
    "start:build":"tsc -w --project tsconfig.json && npx -p tsconfig.json",
    "build:live":"nodemon --watch 'src/**/*' --exec ts-node --esm src/main.ts --verbose",
    "start":"npm run build:live" 
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fastify": "^4.26.0"
  },
  "devDependencies": {
    "@types/node": "^20.11.16",
    "nodemon": "^3.0.3",
    "ts-node": "^10.9.2",
    "typescript": "^5.3.3"
  }
}

 

 

{
  "compilerOptions": {
    "module": "ES2022",                                /* Specify what module code is generated. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "target": "ES2022",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "moduleResolution": "Node",                     /* Specify how TypeScript looks up a file from a given module specifier. */
    "outDir": "./dist",                                   /* Specify an output folder for all emitted files. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    "noFallthroughCasesInSwitch": true,               /* Enable error reporting for fallthrough cases in switch statements. */
    "isolatedModules": true,                          /* Ensure that each file can be safely transpiled without relying on other imports. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "noImplicitAny": true,                            /* Enable error reporting for expressions and declarations with an implied 'any' type. */
    "useUnknownInCatchVariables": true,               /* Default catch clause variables as 'unknown' instead of 'any'. */
    "inlineSourceMap": true                          /* Include sourcemap files inside the emitted JavaScript. */
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "document",
    "backup"
  ],
  "ts-node":{
    "esm":true,
    "experimentalSpecifierResolution":"node"
  }
}

터미널에서 node -v으로 현재 사용하고 있는 nodejs 버전도 확인 부탁드립니다.

강의에 말한 것 처럼 현재는 18버전의 node를 사용하고 있습니다.

그리고 추가로 현재 학습하고 있는 설치한 프로젝트 폴더를 복사해서 보내주시면 제가 한번 살펴보도록 하겠습니다.

git에 올리셔도 되고 구글클라우드에 올리고 링크를 알려주셔도 되니

해당 파일들을 보내주시기 바랍니다

확인해 본 결과 윈도우 화경에서 nodemon하고 ts-node로 실행시키는데 문제가 있는 것 같습니다

그래서 윈도우의 경우

다음과 같이 tsx 모듈을 설치하고

npm i -D tsx

 

package.json에서 build:live를 다음과 같이 수정하고 실행하시면 되겠습니다.

  "scripts": {
    "start:build": "tsc -w --project tsconfig.json && npx -p tsconfig.json",
    "build:live": "npx tsx src/main.ts",
    "start": "npm run build:live"

 

하지만 이렇게 실행할 경우 nodemon의 도움을 받을 수 없어서 코드가 변경후에는

서버를 다시한번 실행해야만 합니다.

 

이후에 윈도우에서도 nodemon과 ts-node로 실행할 수 있다면 확인해서 다시 업데이트 하도록 하겠습니다.

이부분에 대해 혼란을 드려서 죄송합니다.

 

위사항을 참고하시고 혹시 계속해서 문제가 있다면 다시한번 문의 부탁드리겠습니다.

감사합니다.

 

감사합니다. 해결되었습니다 ㅎㅎ