【问题标题】:Cant run my nodejs and typescript project, and I am getting ".ts as an unknown file extension"无法运行我的 nodejs 和 typescript 项目,并且我得到“.ts 作为未知文件扩展名”
【发布时间】:2021-05-27 01:29:58
【问题描述】:

我现在收到此错误:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

这发生在将"type": "module" 添加到package.json 之后

package.json

{
  "name": "Test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "watch": "tsc -w",
    "dev": "tsc && concurrently \"tsc -w\" \"nodemon\""
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "typeorm": "^0.2.31"
  },
  "devDependencies": {
    "@types/node": "^14.14.25",
    "ts-node": "^9.1.1",
    "typescript": "^4.1.4"
  },
  "type": "module"
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
    "removeComments": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "baseUrl": "."
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "./src/**/*.tsx",
    "./src/**/*.ts"
  ]
}

我正在运行版本 nodejs - v14.16.0 和 typescript - v4.1.4

【问题讨论】:

标签: node.js typescript npm


【解决方案1】:

AFAIK allowSyntheticDefaultImportsesModuleInterop 是互斥的。 不过,不知道这是否是真正的问题。另外我会删除包含和排除块,在这种情况下,我认为他们什么都不做。

并仔细检查您是否需要 dist 文件夹中的模块。

【讨论】:

    猜你喜欢
    • 2022-11-04
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2022-08-19
    • 2022-07-23
    • 2020-04-03
    • 2022-08-18
    • 2013-01-18
    相关资源
    最近更新 更多