Support Forums

Intermittent file not found error?

I’m getting file not found errors every other invocation.
Directory is netlify/functions/generate-excel/generate-excel.js & netlify/functions/generate-excel/template.xlsx

The code I have deployed is below. I want to populate a excel template with data from a json, but every other invocation errors out. Not sure why it works sometimes and not other times.

try {
    const resolved = (process.env.LAMBDA_TASK_ROOT)? path.resolve(process.env.LAMBDA_TASK_ROOT, 'template.xlsx'):path.resolve(__dirname, 'template.xlsx');
    /// Open the workbook
    let workbook = new Excel.Workbook();
    workbook.xlsx.readFile(resolved).then(function () {
        let worksheet = workbook.getWorksheet('Sheet1');
        worksheet.eachRow({includeEmpty: true}, function (row, rowNumber) {

    return {
        statusCode: 200,
        body: JSON.stringify({message: 'successful invocation'}),

} catch (error) {
    return {statusCode: 500, body: error.toString()}

Hey there! Interesting use case… not something I’ve seen before.

There’s lots of variables at play here, and I must preface that we don’t debug custom code, I’m afraid. Have you checked out the docs for the dep which you’re using to create the Excel workbook?

Could you try simplifying things, using only fs and .csv files to prove your concept, before introducing Excel workbooks etc? You can create new files in your function:

fs.writeFileSync(file.csv, "[your content]");

Something like that may be enough to get you up-and-running!