javascript – How to bring an array that is in a separate file to the application?

Question:

I have a JS file (data.js) that contains an array with the information below:

 module.exports = [
        {
          "photo": "https://a0.muscache.com/im/pictures/e6c4b347-49c7-4840-8c00-df36a2a273da.jpg?aki_policy=x_large",
          "property_type": "Apartamento",
          "name": "Apartment in Son Parc, wonderful views",
          "price": 433
        },
        {
          "photo": "https://a0.muscache.com/im/pictures/4a5326cb-95e4-4220-a4d8-c91f50cf784c.jpg?aki_policy=xx_large",
          "property_type": "Apartamento",
          "name": "APARTAMENTO IDEAL PAREJAS EN SON PARC",
          "price": 368
        },
      ]

and I need to use this information in the HTML. How do I get JavaScript to recognize this file? I'm trying this command, but it gives an error.

const dados = require('./dados.js');
console.log(dados);

I'm doing the console.log to test if I brought the information, then I'll continue the work.

Answer:

Editing the answer as commented below:

<html>
  <head>    
    <script type="module" src="index.js"></script>
  </head>
  <body>    
  </body>
</html>

index.js

import dados from '/dados.js';

console.log(dados);

data.js

export default [
  {
    "photo": "https://a0.muscache.com/im/pictures/e6c4b347-49c7-4840-8c00-df36a2a273da.jpg?aki_policy=x_large",
    "property_type": "Apartamento",
    "name": "Apartment in Son Parc, wonderful views",
    "price": 433
  },
  {
    "photo": "https://a0.muscache.com/im/pictures/4a5326cb-95e4-4220-a4d8-c91f50cf784c.jpg?aki_policy=xx_large",
    "property_type": "Apartamento",
    "name": "APARTAMENTO IDEAL PAREJAS EN SON PARC",
    "price": 368
  },
]
Scroll to Top