0

I am trying to import a JSON object from a js file in my React project. I am a newbie to React so please go easy :). I keep getting an error no matter what path I change it to (cannot be found). I've attached a screengrab of the directory structure. I am in the scripts/components folder in the home.js file. Thanks. Data is the object in the JSON file imported from the "server" folder.

import React from 'react';
import data from '../server/data';


class Home extends React.Component {
render() {
    return (
        <section id="home">
            <div className="content">
                <p>Test...</p>
            </div>
        </section>
    );
  }
}

// Export out the React Component
module.exports = Home;

enter image description here

3
  • 1
    your file is on server side, move it into the app folder Commented Sep 4, 2019 at 15:31
  • Can you show us how data.js look like? Do you export it? Commented Sep 4, 2019 at 15:34
  • 2
    this should solve import data from '../../../server/data' if you are using export default. Commented Sep 4, 2019 at 15:34

1 Answer 1

1

You have to go three directories up.

  1. ../ takes you to scripts.
  2. ../../ takes you to app.
  3. ../../../ takes you to the project root.

thus,

import data from '../../../server/data';
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.