How to make Tyscript read a JSON file that is on disk?

0

I have a TypeScript (NodeJS-10.6.0) code and need to create a variable that will retrieve the contents of a JSON file.

I've tried to import the contents of the file in the following ways:

 1. import { orderPayload } = require('../../events/qa/myfile.json');
 2. import { orderPayload } = '../../events/qa/myfile.json';
 3. const orderPayload: Order = require ('../../events/qa/myfile.json');
 4. const orderPayload= require ('../../events/qa/myfile.json');

TSlint shows the following errors:

 1. TS1141 - String Literal expected, Required statement not part of a import statement (no-var-required)
 2. String Literal expected
 3. Required statement not part of a import statement (no-var-required)
 4. Required statement not part of a import statement (no-var-required)

Is there any way to import the contents of this JSON file into the orderPayload variable without having to use the NodeJs FS file system module?

    
asked by anonymous 14.08.2018 / 15:17

1 answer

1

You can not simply import into a JSON file without the proper exports.

Do the following:

myfile.json

export const orderPayload  = {...}

And then you can do import { orderPayload } from '../../events/qa/myfile.json';

    
14.08.2018 / 15:36