I am using the latest version of Visual Studio Code. VS Code is using Typescript v3.3.3. I have installed the following packages via npm both locally (save-dev) and globally:
I have also created a tsconfig.json file and added the property - "resolveJsonModule: true"
I have created a config.json file and it is being picked up correctly in my .ts file correctly after adding the resolveJsonModule property above -
Import config from './config.json';
However whenever I run my script I get the following error:
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)
I've already tried the solutions provided here: Typescript compiler error when importing json file
But none of them work for me.
According to this longstanding issue: https://github.com/DevExpress/testcafe/issues/1845 TestCafe uses its own TypeScript configuration, and doesn't honor any tsconfig.json that you use.
So it seems to me that your options are:
resolveJsonModule
flag, and only let TestCafe see the resulting JavaScript code. TypeScript's outDir
flag may be helpful here for copying the files to the right place.MyConfig
) describing the contents of your configuration, and import the JSON file with let json: MyConfig = require('./config.json');
Not as good as letting TypeScript figure things out itself, but it should work around TestCafe's limitations.