Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
Error: Module name "fs" has not been loaded yet for context. Trying to write in a JSON file

Hi everyone c: I've been working in a Node.js project where I bring data from a JSON file and use it with an API to bring weather, and other things, I'm trying to write the new data into another JSON file using require.js but it does not seems to work, here is my code, hope you can help me :)

const fs = require('fs');
var myObject = {  
                Latitud: data.coord.lat,
                Longitud: data.coord.lon,
                Temperatura: data.main.temp, 
                Ciudad: data.name, 
                Humedad: data.main.humidity,
                Descripción: data.weather[0].description,
                Viento: data.wind.speed,
            };
            // convert JSON object to a string
            const _myObject = JSON.stringify(myObject);
            // write file to disk
            fs.writeFile('../js/datareceived.json', _myObject, 'utf8', (err) => {
                if (err) {
                    console.log(`Error writing file: ${err}`);
                } else {
                    console.log(`File is written successfully!`);
                }

            });

and here is the full script

async function calcWeather(){ 
    const info = await fetch('../js/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 fs = require('fs');
        const base = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${api_key}&units=metric&lang=sp`;
        
        fetch(base)
        .then((responses) => {
        return responses.json();
        })
        .then((data) => {
            var myObject = {  
                Latitud: data.coord.lat,
                Longitud: data.coord.lon,
                Temperatura: data.main.temp, 
                Ciudad: data.name, 
                Humedad: data.main.humidity,
                Descripción: data.weather[0].description,
                Viento: data.wind.speed,
            };
            // convert JSON object to a string
            const _myObject = JSON.stringify(myObject);
            // write file to disk
            fs.writeFile('../js/datareceived.json', _myObject, 'utf8', (err) => {
                if (err) {
                    console.log(`Error writing file: ${err}`);
                } else {
                    console.log(`File is written successfully!`);
                }

            });
        });
        
    }
    
};

and here is the way I load in my html

<script src = "./node_modules/requirejs/require.js"></script>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!