score:1

Accepted answer

If you want to run your code as a node app you have to load json with the full url:

const d3 = require('d3');
//use full url to where the json file is
d3.json('http://localhost:8080/jsonfile.json', (err, data) => {
  if (err) {
    console.log(err)
  }
  data.keys.map((t) => {
    console.log(t)
  })
});

If you want to run the code as a node app and load the file from disk you could load the file like so:

fs = require('fs');
const readFile = (file,encoding) => 
  new Promise(
    (resolve,reject)=>
      fs.readFile(
        file
        ,encoding
        ,(err,data) => {
          if(err !== null){
            reject(err);return;
          }
          resolve(data);
        }
      )
    )
;

readFile("./static/jsonfile.json",'utf8')
.then(
  data =>
    data.toString()
).then(
  text =>
    console.log("Got file content:\n",text)
  ,err =>
    console.error("Failed to load file:",err)
);

If your code runs in a browser but you want to use npm as dependency management then you can use requirejs (old way) or webpack (better but a bit more complicated to learn and set up). If you are using your code in a browser I wonder how you do not get an error on const d3 = require('d3');.


Related Query

More Query from same tag