0

I want to include json file on javascript in node application. I have include using "require();". But i have face error:

"Uncaught ReferenceError: require is not defined".

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Fabi
  • 161
  • 1
  • 6
  • No on client side Javascript you can't use `require` you can make `ajax` call to get JSON file's data. – Manwal Oct 08 '15 at 07:35
  • see [http://stackoverflow.com/questions/7163061/is-there-a-require-for-json-in-node-js](http://stackoverflow.com/questions/7163061/is-there-a-require-for-json-in-node-js) – markyph Oct 08 '15 at 07:43
  • also if you are getting undefined - are you running this in a browser or as a server application? see [http://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined](http://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined) – markyph Oct 08 '15 at 07:50

1 Answers1

1

In client side Javascript require won't work. Instead of this use jQuery.getJson()

$.getJSON( "json_file.json", function( data ) {
   //data variable having your json data 
});
Manwal
  • 23,450
  • 12
  • 63
  • 93
  • `$.getJSON` in `nodejs` ? – Rayon Oct 08 '15 at 07:39
  • 1
    @RayonDabre: This is an xy question. The OP complains that javascript complains that `require` is not defined. That means he's not running his script in node. The OP is confused. This answer is probably correct. – slebetman Oct 08 '15 at 08:06
  • Yes OP mentioned nodejs but actually he is trying to access JOSN at client side. @RayonDabre OP is confuse b/w Node and Javascript so he is trying Node's syntax in JS. – Manwal Oct 08 '15 at 08:14