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

439
Views
How to wait until a file is created before trying to download it in Node.js

I have a web app built using node and express that uses a python script to generate an excel file. The issue is that although node is successfully handling the request to create the excel file, the handler is also trying to download the file just created but is giving the error: Error: ENOENT: no such file or directory. I'm assuming this is because it is trying to access the file before it is created but I thought that by using await the file would be created first. Here is my code:

Routes:

router.post('/executepythonscript', db.executePythonScript)

Handler:

const executePythonScript = async (request, response) => {
    const { spawn } = require('child_process');

    let pythonPromise = new Promise((resolve, reject) => {
        const python = spawn('py', ['./scripts/generate_excel.py', request.body.week]);
        
        python.stdout.on("data", (data) => {
            resolve(data.toString());
        });
        python.stderr.on("data", (data) => {
            reject(data.toString());
        });
        python.on('close', (code) => {
            console.log(`child process exited with code ${code}`);
        });
    });

    try {
        await pythonPromise;
    } catch (error) {
        console.log(error);
    }

    response.download('[myPath]/timetracking.xlsx');
}

Am I using await wrong or why would the program try to open the file before it is created?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Get rid of the try / catch statement in the function and add the response.download() to python.on(close). It should look like this:

const { spawn } = require('child_process');

const python = spawn('py', ['./scripts/generate_excel.py', request.body.week]);
python.stderr.on("data", (data) => {
   console.log(data.toString());
});
python.on('close', (code) => {
   console.log(`child process exited with code ${code}`);
   response.download('[Path]/tracking/timetracking.xlsx');
});
about 4 years ago · Juan Pablo Isaza Report
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!