javascript - How to require .json file with module.exports in nodeJS -
im used creating .json files way
module.exports = [ {"year":2007,"month":1,"startdate":"2007-01-01","enddate":"2007-01-31"}, {"year":2007,"month":2,"startdate":"2007-02-01","enddate":"2007-02-28"}, {"year":2007,"month":3,"startdate":"2007-03-01","enddate":"2007-03-31"}, ]
i require them this.
var dates = require('./json/dates.json');
this has worked in past when working nodejs , grunt build websites. im using nodejs create server app message
syntaxerror: g:\navision reports\js reportserver\json\dates.json: unexpected token message. dont understand what's going on. working .js files.
please knows why no longer works? know have json object if remove var dates = require('./json/dates.json'); , make file 1 json object, i'd rather not reorganise data.
this isn't valid json file, can check in code formatter has syntax errors:
module.exports = [ {"year":2007,"month":1,"startdate":"2007-01-01","enddate":"2007-01-31"}, {"year":2007,"month":2,"startdate":"2007-02-01","enddate":"2007-02-28"}, {"year":2007,"month":3,"startdate":"2007-03-01","enddate":"2007-03-31"}, ]
please format json in valid way , require or change dates.json
dates.js
.
if save file .json
don't have use module.exports
, can't, because node has built-in support requiring .json
files. don't have use json.parse
.
Comments
Post a Comment