You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行npm start时Node.js提示.ts文件扩展名未知的问题

问题:Node.js运行TS服务提示未知文件扩展名.ts

我原本使用Express编写了一个小型JS服务脚本,之后决定将其转为TS,但完成转换后,每当运行npm start时,Node.js都会报错提示未知文件扩展名.ts。

报错信息

PS C:\Users\aryan\OneDrive\Documents\GitHub\spacious-forms> npm start

> spacious-forms@0.0.0 start
> npx ts-node --esm src/server.ts

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\Users\aryan\OneDrive\Documents\GitHub\spacious-forms\src\server.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:141:22)
    at async nextLoad (node:internal/modules/esm/hooks:749:22)
    at async nextLoad (node:internal/modules/esm/hooks:749:22)
    at async Hooks.load (node:internal/modules/esm/hooks:382:20)
    at async MessagePort.handleMessage (node:internal/modules/esm/worker:199:18) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

package.json 文件内容

{
  "name": "spacious-forms",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "start": "npx ts-node --esm src/server.ts",
    "dev": "vite",
    "build": "vue-tsc && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "vue": "^3.3.8"
  },
  "devDependencies": {
    "@types/express": "^4.17.21",
    "@types/md5": "^2.3.5",
    "@types/node": "^20.10.0",
    "@vitejs/plugin-vue": "^4.5.0",
    "express": "^4.18.2",
    "md5": "^2.3.0",
    "sqlite3": "^5.1.6",
    "tailwindcss": "^3.3.5",
    "ts-node": "^10.9.1",
    "typescript": "^5.3.2",
    "vite": "^5.0.0",
    "vue-tsc": "^1.8.22"
  }
}

tsconfig.json 文件内容

{
  "compilerOptions": {
    "esModuleInterop": true,
    "useDefineForClassFields": true,
    "module": "ESNext",
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "skipLibCheck": true,
    "paths": {
      "@/*": [
        "./*"
       ]
    },
    /* Bundler mode */
    "moduleResolution": "node",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true
  },
  "ts-node": {
    "esm":true
  },
  "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "../src/vite-env.d.ts", "../src/main.ts", "vue.d.ts", "vue.d.ts"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

补充说明

已查阅资料并尝试了各种修复方法,包括修改ES版本、使用ts-node等,但均无效。已将tsconfig.json中的moduleResolution从"node"改为"Node",但仍出现相同错误。


内容的提问来源于stack exchange,提问作者SpaciousCoder78

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 11:05:12