1

I have used create-react-app to create a reactjs project. I need to import a JSON file into .js files which is outside of the src folder. Reason for this is the JSON files contains some of configurations. after I finish the build, I can change the configurations without re-build it. SO it is very important for me to keep the JSON file in the root folder.

without ejecting is there anyway that i can do it?

Chamod Pathirana
  • 713
  • 8
  • 16
  • You can import it from wherever you want by entering the full path, but it's not a good idea to keep important files outside. It would be better if you put it in the src folder and copy it when you build. – Superblar Dec 27 '19 at 05:05
  • @Superblar, where to put the copied file? how can I give the path? – Chamod Pathirana Dec 27 '19 at 05:33
  • Does this answer your question? [Import JSON file in React](https://stackoverflow.com/questions/39686035/import-json-file-in-react) – Yash Karanke Dec 27 '19 at 06:13
  • 1
    @ChamodPathirana You have to put it in the public(build) folder. Then you can load the data from the file using [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch). – Superblar Dec 27 '19 at 18:24

1 Answers1

0

Note that you need to keep the JSON file in your "public" folder, so then you can use it in your local, like ex. localhost:3000/api.json. in this case you fetch your json file and use the data. And it must not give any error while building. IF you kepp your JSON file in your src folder in that case you need to import that file in your project.