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

1
  • How are you building your bundle? Commented Aug 5, 2016 at 8:49

1 Answer 1

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"}
]
Sign up to request clarification or add additional context in comments.

2 Comments

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´
Could you please provide your json file?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.