rufreakde
rufreakde

Reputation: 632

JavaScript ES6 JSON import syntax

I try to write my code in ES6 standard but when I try to import a .json it fails on me.

import JsonT from "../../Data/t.json" //not working

var JsonT = require('../../Data/t.json'); //working

Can someone explain what I do wrong?

I use it then like this:

console.log(JasonT);

Upvotes: 1

Views: 1359

Answers (1)

cathalkilleen
cathalkilleen

Reputation: 315

Since ES6 is not actually supported on most browsers or JS platforms yet, you will need to use a 'transpiler' like Babel to convert your ES6 code into interpretable JavaScript.

If you are using Webpack, you can use babel-loader to manage this 'transpiling' for you.

If you are just getting started with React, I would recommend using a build config starter like create-react-app. This will allow you to get started writing ES6 React code without having to worry about the initial overhead of setting up a build environment.

Upvotes: 1

Related Questions