title: How to import fetch to my app.js in NodeJS?
body: <p>I'm working on a project in NodeJS that takes coords from a JSON <code>data.json</code> and then uses that coords to bring the weather, and then that data of the weather, to write it into another JSON.
  The issue here is that my function uses and wait fetch, and that gives me an error that says <strong>fetch is not defined</strong> so, I did what a question here said, I downloaded fetch, I imported it and now its says that I need to type
  <code>&quot;type&quot;: &quot;module&quot;</code> in my <code>package.json</code>, so I did and now it says that my <code>var fs=require('fs'</code> is not defined :c I'm using filesystem to create that another JSON, any help is welcomed, thank you c: here's my <code>app.js</code></p>
  <pre><code>var fs = require('fs')
  import fetch from &quot;node-fetch&quot;;
  
  const api_key = '******'
  
  async function calcWeather(){ 
      const info = await fetch('./data.json') // fetch editable c:
      .then(function(response) {
          return response.json();
      });    
      for (var i in info) {
          const lat = info[i][0].latjson;
          const long = info[i][0].lonjson;
          const base = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&amp;lon=${long}&amp;appid=${api_key}&amp;units=metric&amp;lang=sp`;
          
          fetch(base)
          .then((responses) =&gt; {
          return responses.json();
          })
          .then((data) =&gt; {
              var myObject = {  
                  Latitud: data.coord.lat,
                  Longitud: data.coord.lon,
                  Temperatura: data.main.temp, 
                  Ciudad: data.name, 
                  Humedad: data.main.humidity,
                  Descripcion: data.weather[0].description,
              };
              // convert JSON object to a string  
              const _myObject = JSON.stringify(myObject);
              /* console.log(JSON.stringify(myObject)) */
              // write file to disk
              fs.writeFileSync('data2.json', _myObject, 'utf8', (err) =&gt; {
                  if (err) {
                      console.log(`Error writing file: ${err}`);
                  } else {
                      console.log(`File is written successfully!`);
                  }
              });
              fs.writeFile('data.txt', _myObject,'utf8', (err) =&gt; {
                  if (!err){
                      console.log(`Written succesfully! c:`)
                  }
              })
          });   
      }   
  };
  calcWeather()
  </code></pre>
  <p>and here's my JSON</p>
  <pre><code>[
      [
        {
          &quot;latjson&quot;: 21.1524,
          &quot;lonjson&quot;: -101.7108
        }
      ],
      [
        {
          &quot;latjson&quot;: 19.4328, 
          &quot;lonjson&quot;:  -99.1346
        }
      ],
      [
        {
          &quot;latjson&quot;: 20.6605,
          &quot;lonjson&quot;: -103.3525
        }
      ]
    ]
  </code></pre>
  <p>and here's my package.json c:</p>
  <pre><code>  &quot;name&quot;: &quot;node-js-app&quot;,
    &quot;version&quot;: &quot;1.0.0&quot;,
    &quot;description&quot;: &quot;&quot;,
    &quot;main&quot;: &quot;index.js&quot;,
    &quot;scripts&quot;: {
      &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;
    },
    &quot;author&quot;: &quot;&quot;,
    &quot;license&quot;: &quot;ISC&quot;,
    &quot;dependencies&quot;: {
      &quot;ejs&quot;: &quot;^3.1.6&quot;,
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;node-fetch&quot;: &quot;^3.0.0&quot;
    }
  }
  </code></pre>