1

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);
7
  • 1
    import is not supported by Node.JS yet. You can use Babel if you want to use that. Commented Aug 8, 2017 at 7:02
  • stackoverflow.com/questions/34944099/… Commented Aug 8, 2017 at 7:02
  • @barbsan that question is not about Node.js Commented Aug 8, 2017 at 7:03
  • actually i work with ReactJS sorry for the misleading tag? Commented Aug 8, 2017 at 7:07
  • 1
    @LEQADA apparently this question also is not about Node.js. @Rudi Have you tried import JsonT from "../../Data/t" as suggested in linked question? Commented Aug 8, 2017 at 7:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.