Using JSON files with Javascript on the web

Asked

Viewed 54 times

-4

i have a huge array with numerous objects, wondered if I can export to my main JS file?

have already done:

module.Exports = filename

but when I open in the browser appears the error message: 'require is not defined'

  • Lucas, do you happen to be running your code with node.js ?

  • Do you use module.Exports in another request? Put the code that makes it easy to understand

  • I’m not running with nodejs, I was wondering if I can export an array from another JS file only running in the browser

2 answers

0

If you are using Node just use require in the json file

const array = require('path./file.json');

in the browser you can use the fetch or ajax to request the file on the server:

const array = await fetch('path/file.json').then(r => r.json());

-3

Man by what I understood if you have a.html file and inside it you have a.js script and if you want the script to be able to access a file with external data, right?

If that is it will be necessary to put the file in your html code, but maybe you want to evaluate a change (update in your code) that if it is what I understood is very archaic

Browser other questions tagged

You are not signed in. Login or sign up in order to post.