Skip to content
Advertisement

Import JSON file in React

I’m new to React and I’m trying to import a JSON DATA variable from an external file. I’m getting the following error:

Cannot find module “./customData.json”

Could some one help me? It works if I have my DATA variable in index.js but not when it’s in an external JSON file.

index.js
JavaScript
hobbies.js
JavaScript
profile.js
JavaScript
customData.json
JavaScript

Advertisement

Answer

One nice way (without adding a fake .js extension which is for code not for data and configs) is to use json-loader module. If you have used create-react-app to scaffold your project, the module is already included, you just need to import your json:

JavaScript

This answer explains more.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement