【问题标题】:TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"类型错误 [ERR_UNKNOWN_FILE_EXTENSION]:未知文件扩展名 \".ts\"
【发布时间】:2022-08-18 05:27:54
【问题描述】:

当我运行测试命令时,我正在使用 mocha \"mocha\": \"^7.2.0\", 进行单元测试:

\"test\": \"cross-env TS_NODE_PROJECT=\'test/tsconfig.test.json\' mocha test/**/**\",

显示如下错误:

➜  js-wheel git:(main) ✗ yarn test
yarn run v1.22.17
$ cross-env TS_NODE_PROJECT=\'test/tsconfig.test.json\' mocha test/**/**

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension \".ts\" for /Users/xiaoqiangjiang/source/reddwarf/frontend/js-wheel/test/utils/time.test.ts
    at new NodeError (node:internal/errors:371:5)
    at Object.file: (node:internal/modules/esm/get_format:72:15)
    at defaultGetFormat (node:internal/modules/esm/get_format:85:38)
    at defaultLoad (node:internal/modules/esm/load:13:42)
    at ESMLoader.load (node:internal/modules/esm/loader:303:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:230:58)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:244:11)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async Object.exports.loadFilesAsync (/Users/xiaoqiangjiang/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/esm-utils.js:28:20)
    at async singleRun (/Users/xiaoqiangjiang/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/cli/run-helpers.js:149:3)
    at async exports.runMocha (/Users/xiaoqiangjiang/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/cli/run-helpers.js:186:5)
    at async Object.exports.handler (/Users/xiaoqiangjiang/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/cli/run.js:319:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

有人告诉我可以删除package.json 中的\"type\": \"module\",但我无法删除它,我需要使用es6 导入。我检查了 tsconfig.json 并且 esModuleInterop 配置已经是真的。我应该怎么做才能解决这个问题?这是tsconfig.json

{
  \"compilerOptions\": { 
    /* Language and Environment */
    \"target\": \"es5\",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    
    /* Modules */
    \"module\": \"esnext\",                                /* Specify what module code is generated. */
    
    /* Emit */
    \"declaration\": true,                              /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
    \"outDir\": \"./dist\", 
    \"lib\": [ \"DOM\",\"ES2015\" ], /* https://stackoverflow.com/questions/43555378/ts-an-async-function-or-method-in-es5-es3-requires-the-promise-constructor */
    \"esModuleInterop\": true, 
    \"moduleResolution\":\"node\",                            /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
    // \"preserveSymlinks\": true,                         /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
    \"forceConsistentCasingInFileNames\": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    \"strict\": true,                                      /* Enable all strict type-checking options. */
    \"skipLibCheck\": true,
    \"baseUrl\": \"src\",
    \"paths\": {
      \"@net/*\":[\"net/*\"],
      \"@auth/*\":[\"auth/*\"],
      \"@utils/*\":[\"utils/*\"],
      \"@model/*\":[\"model/*\"],
    },
    // Note: To transform paths for both the output .js and .d.ts files, you need both of the below entries
    \"plugins\": [
      // Transform paths in output .js files
      { \"transform\": \"typescript-transform-paths\" },
      { \"transform\": \"ts-transformer-keys/transformer\" },

      // Transform paths in output .d.ts files (Include this line if you output declarations files)
      { \"transform\": \"typescript-transform-paths\", \"afterDeclarations\": true }
    ]                                 
  },
  \"exclude\": [
    \"dist\"
  ]
}

我也试过这个命令:

\"test\": \"mocha -r ts-node/register test/**/**\",

仍然没有工作。还尝试使用 ts-mocha:

\"test\": \"ts-mocha -p tsconfig.json test/**/**\",

不工作。调整 tsconfig.json:

\"module\": \"CommonJS\"

仍然没有工作。

  • 您正在设置TS_NODE_PROJECT,但直接运行mocha,而不是ts-mochats-nodenode -r tsnode/register。是否有其他一些配置,您希望 Node 在没有显式配置的情况下处理 TypeScript?

标签: typescript mocha.js


【解决方案1】:

这解决了吗?如果是这样,解决方案是什么?现在也遇到这个问题。

【讨论】:

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