2

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

Community
  • 1
  • 1
user1937021
  • 10,151
  • 22
  • 81
  • 143

1 Answers1

5

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"}
]
Alexandr Lazarev
  • 12,554
  • 4
  • 38
  • 47
  • I did this but now I get ´ERROR in ./~/json-loader!./src/json/acordes.json Module build failed: SyntaxError: Unexpected token m at Object.parse (native) at Object.module.exports (/Users/alexanderlloyd/Documents/tocacuatro/node_modules/json-loader/index.js:7:48) @ ./src/js/App.js 66:11-47´ – user1937021 Aug 05 '16 at 09:30
  • Could you please provide your json file? – Alexandr Lazarev Aug 05 '16 at 09:37