How to use a JSON file in TypeScript?


require is meant for loading your modules; the recommended way to load files (including the JSON ones) is through Node's filesystem module.

You could do something like this:

// Declare your variables
var fs = require('fs');
var menObject;

// Read the file, and pass it to your callback
fs.readFile('./men.json, handleJSONFile);

// Handle the data 
var handleJSONFile = function (err, data) {
    if (err) {
        throw err;
    menObject = JSON.parse(data);
Show all replies

@rezarazavipour I have not worked with Angular 2, but it seems you are using System.js for loading the modules; this presumption is based on a quick search of your error, which has brought up the following solution:

System.js overrides Node.js's require method and uses it's own resolution mechanism. So instead of using require, you would use System._nodeRequire('fs')

Reply to this…

(3 answers) Take me to the question