【问题标题】:TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" on Visual Studio CodeTypeError [ERR_UNKNOWN_FILE_EXTENSION]:Visual Studio Code 上的未知文件扩展名“.ts”
【发布时间】:2020-12-02 18:21:23
【问题描述】:

我一直在对一些代码进行单元测试,但似乎不知从何而来,我收到有关未知文件扩展名的错误(之前,我收到有关无法识别某些类型的错误)。

这是我的 package.json:

{
  "name": "cgt-core-engine-mv",
  "description": "API container for other CGT plugins",
  "version": "1.01.03",
  "author": "CG-Tespy",
  "repository": {
    "type": "git",
    "url": "https://github.com/CG-Tespy/CGT_CoreEngine_MV"
  },
  "scripts": {
    "build": "tsc & (rollup -c)",
    "test": "mocha -r ts-node/register Test/**/*.ts"
  },
  "dependencies": {},
  "devDependencies": {
    "@types/chai": "^4.2.12",
    "@types/mocha": "^8.0.0",
    "@types/sinon": "^9.0.4",
    "chai": "^4.2.0",
    "mocha": "^8.0.1",
    "rollup": "^2.00.1",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-pluginutils": "^2.8.2",
    "sinon": "^9.0.2",
    "ts-node": "^8.10.2",
    "typescript": "^3.9.6"
  },
  "type": "module"
}

这是我的 tsconfig.json:

{
    "compilerOptions": {
      "target": "es5",
      "module": "CommonJS",
      "esModuleInterop": true,

      "sourceMap": false,
      "downlevelIteration": true,
    },
    "include": ["_MainSource/**/*", "Tests/**/*", "@typeDefs/**/*"],
}

我真的不知道这是从哪里来的。我有一个单独的项目,我能够很好地对事物进行单元测试,并且没有给我任何错误。它的 package.json:

{
  "name": "unittesting",
  "version": "1.0.0",
  "description": "For practicing unit-testing",
  "main": "main.js",
  "scripts": {
    "test": "mocha -r ts-node/register test/**/*.ts"
    
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/chai": "^4.2.11",
    "@types/mocha": "^8.0.0",
    "chai": "^4.2.0",
    "mocha": "^8.0.1",
    "ts-node": "^8.10.2",
    "typescript": "^3.9.7"
  },
  "type": "module"
}

它的 tsconfig:

{
  "compilerOptions": {
    /* Basic Options */

    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "CommonJS",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    
    "esModuleInterop": true,
    
  },
  "include": ["src/**/*", "tests/**/*"],
}

什么可能导致此问题?我已经尝试了here 的建议,但没有奏效(即使在查看了链接的 Github 问题之后)。

【问题讨论】:

标签: typescript unit-testing visual-studio-code


【解决方案1】:

您是否尝试过从 package.json 文件中删除 "type": "module"

【讨论】:

  • 正如我在对 OP 的评论回复中解释的那样,这样做会导致我无法在模块外使用 import 语句的错误
  • 谢谢,我没有找对方向,你省了我一些头疼 :)
猜你喜欢
  • 2022-11-04
  • 2022-08-18
  • 2022-08-19
  • 1970-01-01
  • 2022-09-23
  • 2018-04-14
  • 2019-02-09
  • 2021-11-22
  • 2022-11-04
相关资源
最近更新 更多