How to import a json file in ecmascript 6?

Nikita Jajodia picture Nikita Jajodia · Jan 22, 2016 · Viewed 202k times · Source

How can I access a json file in Ecmascript 6 ? The following doesn't work:

import config from '../config.json'

This works fine if I try to import a JavaScript file.

Answer

williamli picture williamli · Jun 13, 2018

In TypeScript or using Babel, you can import json file in your code.

// Babel

import * as data from './example.json';
const word = data.name;
console.log(word); // output 'testing'

Reference: https://hackernoon.com/import-json-into-typescript-8d465beded79