Referencing files in functions

I have made a Netlify function that runs with submission-created.js every time a form on my page is created.

I got it working locally with Netlify Dev, but I can’t seem to get it working in production.

The function is sending an auto Reply with NodeMailer to the person that filled out the contact form.

I have 2 separate *.handlebars files I use as templates, but I am not sure how to reference these inside the Functions folder?

I have made a subdirectory inside my functions folder named “autoReply”. Here I have placed both the function AND the 2 *.handlebars files. I know that the function is running isolated on AWS - but I was under the impression I could do it this way?

Right now I am referencing the files inside the function with:

const path = require('path');

“path.resolve(__dirname, “…/autoReply/”),”

In production I get this error:
Invoke Error {“errorType”:“Error”,“errorMessage”:“ENOENT: no such file or directory, open ‘/var/task/src/functions/autoReply/mailTemplateDa.handlebars’”,“code”:“ENOENT”,“errno”:-2,“syscall”:“open”,“path”:"/var/task/src/functions/autoReply/mailTemplateDa.handlebars",“stack”:[“Error: ENOENT: no such file or directory, open ‘/var/task/src/functions/autoReply/mailTemplateDa.handlebars’”]}

What is the correct way to reference files on AWS?

I have looked at various other community posts, but am still unsure how to do this correctly?

Hi @LarsEjaas,

I actually did some testing around this. Here’s an example of a successful test: function-deploy-test/zipped-function.js at master · netlify/function-deploy-test · GitHub. Note that I had to zip up the function myself here: function-deploy-test/package.json at master · netlify/function-deploy-test · GitHub. This is so the extra files will be included in the bundled function because the build environment will not include any files that is not required by your function.

Let me know if that helps.

Thanks Dennis! This is this was excactly what I was looking for! I will try to implement this!

One question though! Could you simply require the file you need to get it included in the build?
I was thinking something along the lines of:

const getThisFile = require("./includeThisAwesomeFile.hbs")

Am I missing something obvious here?

Allright, I simply had to add 2 lines of code as stated above to require the two *. handlebars files for my email template. No zip files needed I guess :blush: But thanks a lot for your helpful answer Denni: I am sure the solution with zip-files is very useful for something more complex than what I needed here :blush: