user1937021
user1937021

Reputation: 10791

Load and display json data in react component

I'm trying to load in data from a json file in a react component. I am trying this solution in my app.js.

https://stackoverflow.com/a/33141549/1937021

var data = require('json!../json/acordes.json');

"Cannot resolve module JSON error" in the terminal.

I do this in the app.js file. My folder structure is as follows:

/src
  /js
    app.js
  /css
  /json
    acordes.json

The rest of my code looks like this:

https://gist.github.com/dabit3/651f2dae058ff99810eb771c2817d622

Upvotes: 2

Views: 3335

Answers (1)

Alexandr Lazarev
Alexandr Lazarev

Reputation: 12882

I may be mistaken, but in order to be able to require json in such a way, you have to use webpack with json-loader.

Assuming, you use webpack. Install json-loader:

npm install --save json-loader

And add to webpack loaders:

//.. 
loaders: [
  {test: /\.json$/, loader: "json", include: "path/to/your/sources"}
]

Upvotes: 5

Related Questions