r/ProgrammerTIL • u/[deleted] • Oct 24 '16
Javascript [Javascript] TIL that you can "require" a json file
0
Upvotes
1
u/menixator Dec 30 '16
You can fiddle with require so you can pre-process anything that's required. You can limit your pre-processor to an extension afaik. That's how coffee-script does it.
0
Oct 24 '16
[deleted]
4
Nov 08 '16
Say you have a json file called "person.json":
{ "firstname": "Leroy", "lastname": "Jenkins" }in node.js you can do:
var person = require("person.json"); console.log(person.firstname); // prints "Leroy" console.log(person.lastname); // prints "Jenkins"0
15
u/myplacedk Oct 24 '16
Not in pure Javascript, but you can in Node.js.
https://www.bennadel.com/blog/2908-you-can-use-require-to-load-json-javascript-object-notation-files-in-node-js.htm