Function unable to load file when deployed

For some reason my HTML file that is an a subdirectory of the functions folder is not found when I deploy to Netlify.

An unhandled error in the function code triggered the following message:

Error - ENOENT: no such file or directory, open '/var/task/templates/mail-after-signup.html'

I’ve implemented solutions from different threads on this community, but they don’t seem to work for me. My setup is pretty simple:

folder structure:

functions\
  mail-after-signup.js
  templates\
     mail-after-signup.html
...
// mail-after-signup.js
const fs = require('fs')
const path = require('path')
const Mustache = require('mustache')
const currentDir = process.env.LAMBDA_TASK_ROOT;

// Tried loading the template in using both approaches below
// const template = fs.readFileSync(require.resolve('./templates/mail-after-signup.html'), 'utf8')
const template = fs.readFileSync(path.join(currentDir, './templates/mail-after-signup.html'), 'utf8')

exports.handler = async function(event, context) {
  return {
    statusCode: 200,
    body: Mustache.render(template.toString('base64'), {
      name: 'Franz Fahrenheit'
    })
  }
} 

And my netlify.toml file:

[functions]
  included_files = ["templates/**"]

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200

Any thoughts why this isn’t working?

1 Like

The included_files need to be relative to the base directory of your folder structure, so in your case ./functions/templates/**