导入config.json文件时,Typescript始终出错

导入config.json文件时,Typescript始终出错,typescript,testing,automated-tests,e2e-testing,testcafe,Typescript,Testing,Automated Tests,E2e Testing,Testcafe,我正在使用最新版本的Visual Studio代码。 VS代码使用的是Typescript v3.3.3。 我已通过npm在本地(保存开发)和全球安装了以下软件包: TestCafe v1.1.0 核心JSV3.0 类型脚本v3.4.1 我还创建了一个tsconfig.json文件,并添加了属性“resolveJsonModule:true” 我已经创建了一个config.json文件,在上面添加了resolveJsonModule属性之后,它在我的.ts文件中被正确地提取了- Import c

我正在使用最新版本的Visual Studio代码。 VS代码使用的是Typescript v3.3.3。 我已通过npm在本地(保存开发)和全球安装了以下软件包:

  • TestCafe v1.1.0
  • 核心JSV3.0
  • 类型脚本v3.4.1
  • 我还创建了一个tsconfig.json文件,并添加了属性“resolveJsonModule:true”

    我已经创建了一个config.json文件,在上面添加了resolveJsonModule属性之后,它在我的.ts文件中被正确地提取了-

    Import config from './config.json';
    
    但是,每当我运行脚本时,都会出现以下错误:

        Error: TypeScript compilation failed.
        /Users/test/Documents/Dev_Node/TestCafe/UK/homepage-fixture.ts (2, 20): 
        Cannot find module './config.json'. Consider using '--resolveJsonModule' 
        to import module with '.json' extension at Function._reportErrors (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/te st-file/formats/typescript/compiler.js:45:15) at TypeScriptTestFileCompiler._reportErrors [as _precompileCode (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/test-file/formats/typescript/compiler.js:79:40) at TypeScriptTestFileCompiler._precompileCode [as_compileCodeForTestFiles (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/te 
    st-file/api-based.js:110:29) at TypeScriptTestFileCompiler._compileCodeForTestFiles [as precompile (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/test-file/api-based.js:169:21) at precompile (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/index.js:70:48) at Compiler._precompileFiles (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/index.js:66:54) at _precompileFiles (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/index.js:105:91) at map (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/i ndex.js:105:41)
    
    我已经尝试过这里提供的解决方案:


    但是它们都不适合我。

    根据这个长期存在的问题:TestCafe使用自己的TypeScript配置,不支持您使用的任何tsconfig.json

    因此,在我看来,您的选择是:

    • 使用自己的tsconfig.json编译TypeScript代码,该tsconfig.json包含
      resolveJsonModule
      标志,并且只让TestCafe看到生成的JavaScript代码。TypeScript的
      outDir
      标志在这里可能有助于将文件复制到正确的位置
    • 创建一个描述配置内容的接口(可能名为
      MyConfig
      ),并使用
      let JSON:MyConfig=require('./config.JSON')导入JSON文件不如让TypeScript自己解决问题,但它应该能够克服TestCafe的局限性

    “考虑使用'--resolveJsonModule'”。是的,我确实看到了这个问题,但错误地认为它已在2月得到修复。我曾尝试安装“require”npm包,但在安装过程中不断出现以下错误
    code ELOCKVERIFY npm ERR!在package-lock.json中发现错误,请运行npm安装来修复这些错误。npm错误!无效:锁定文件的typescript@3.3.3不满足typescript@^3.4.1
    看起来Testcafe的typescript与npm版本冲突?请尝试删除您的
    包锁.json
    并运行
    npm i-D Testcafe typescript
    。Andrey,非常感谢您!!这解决了我的问题,我可以使用require-
    var config=require('./config.json')使用config.json文件。我必须在tsconfig.json中设置属性“moduleResolution”:“node”,并在抛出以下类型脚本错误时运行npm命令:
    找不到名称“require”。是否需要为节点安装类型定义?尝试
    npm i@types/node`,然后将
    node
    添加到tsconfig中的types字段中`