Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

442
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda