Nikita Vlasenko
Nikita Vlasenko

Reputation: 4352

Webpack: SyntaxError: Unexpected token error when trying to access JSON array

I am importing .json from .js file: import pca from './pca.js'. Using console.log() outputs correct JSON in the console, but when I am trying to access it in code

pca_json = JSON.parse(pca); (I can remove parsing, not working either) 
pca_json["rowname"].forEach(... 

webpack fails with an error:

enter image description here

I tried changing extention: .js -> .json but it fails to import the file at all in that case giving me another error:

enter image description here

The pca.js (or pca.json) looks like that:

var pca = {
    "rowname" : [
        "Ciliated_sensory_neurons",
        "Touch_receptor_neurons",
         ...
         ...
    ]
}
export default pca

I tried importing pca.json using fs module but it is not working either. I am getting the error: https://github.com/meteor/meteor/issues/7384

Any suggestions would be greatly appreciated.

Upvotes: 0

Views: 222

Answers (1)

Charlie Martin
Charlie Martin

Reputation: 8406

Your file is Javascript. Not JSON. JSON is just a string. Anything with var in it is Javascript. The issue is with your javascript code in datumScatterChart.js after you import it. Not with the file you are importing.

You are attempting to do a forEach inside a variable declaration. Change datumScatterChart.js to look like this...

var data = [], shapes = [...];

pca_json["rowname"].forEach(function () { ... });

Notice the semicolon after the variable declartion and before the forEach loop. This is exactly what the error message is telling you Unexpected token , expected ;

Upvotes: 1

Related Questions