# Accessing File from Function / Deployment

**URL:** <https://answers.netlify.com/t/accessing-file-from-function-deployment/35370>\
**Category:** Support\
**Tags:** netlify-newbie\
**Created:** [April 3, 2021, 11:53pm UTC](https://answers.netlify.com/t/accessing-file-from-function-deployment/35370 "2021-04-03T23:53:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/steven/32/23695_2.png) [@Steven](https://answers.netlify.com/u/Steven)\
**Post date:** [April 3, 2021, 11:53pm UTC](https://answers.netlify.com/t/accessing-file-from-function-deployment/35370/1 "2021-04-03T23:53:34Z")

</div>

Hi all, a bit new to web development!

I am using Netlify to host my github repo and I am trying to send an attachment via email using nodemailer in my submission-created.js:

```
let replyMailOptions = {
        from: GMAIL,
        to: mail,
        subject: `My Test Subject`,
        html: `<p>a message</p>`,
        attachments: {
            path: './test.txt'
        }
    };

```

but it isn’t locating the file. I’ve tried placing the file under /functions/test.txt, under a subfolder /functions/test/test.txt as well as /assets/test.txt. I’ve tried to view the files under the current folder with

```
fs.readdir(__dirname, (err, files) => {
    files.forEach(file => {
      console.log(file);
    });
  });

```

I’ve also tried to copy the code from here: [netlify-functions-cra-example/read-file.js at master · talves/netlify-functions-cra-example · GitHub](https://github.com/talves/netlify-functions-cra-example/blob/master/functions/read-file/read-file.js) but I am getting file not found. Specifically - "Invoke Error {“errorType”:“Error”,“errorMessage”:“Cannot find module ‘./test.txt’\n”

I keep going down rabbit holes and not sure which path leads me to what I want. Some of these examples being: build configurations (something about my relative/absolute paths?), Netlify’s “backend” options, large media, site generators and I’m getting overwhelmed and feel like I might be overcomplicating a simple solution.

Any help is much appreciated!

---

<div class="post-metadata">

**Author:** ![jen](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jen/32/6044_2.png) [@jen](https://answers.netlify.com/u/jen)\
**Post date:** [April 4, 2021, 12:43am UTC](https://answers.netlify.com/t/accessing-file-from-function-deployment/35370/2 "2021-04-04T00:43:01Z")

</div>

Hey @Steven,  
Here’s an example where I accessed a pdf in a function:

```auto
// function.js

const path = require("path");
const fs = require("fs");

exports.handler = async function () {
  console.log(path.resolve(__dirname + "/rfc2468.pdf"));
  const pathToPDF = path.resolve(__dirname + "/rfc2468.pdf");
  console.log(fs.existsSync(pathToPDF));
  const pdf = fs.readFileSync(pathToPDF);

  try {
    return {
      statusCode: 200,
      headers: { "Content-type": "application/pdf" },
      isBase64Encoded: true,
      body: pdf.toString("base64"),
    };
  } catch (err) {
    return {
      statusCode: 500,
      body: err.toString(),
    };
  }
};

```

Both the function and the file were at the same level in my functions directory, so:

```auto
functions/
  |-thisFunction
  |---- function.js
  |---- file.pdf
  |---- ...
  |-anotherFunction/
...

```

Let us know if something like that would work for you

---

<div class="post-metadata">

**Author:** ![Steven](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/steven/32/23695_2.png) [@Steven](https://answers.netlify.com/u/Steven)\
**Post date:** [April 4, 2021, 2:10am UTC](https://answers.netlify.com/t/accessing-file-from-function-deployment/35370/3 "2021-04-04T02:10:25Z")

</div>

Thanks a lot @jen ! Your example gave me what I needed to line things up and fix my problems.

---

<div class="post-metadata">

**Author:** ![Steven](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/steven/32/23695_2.png) [@Steven](https://answers.netlify.com/u/Steven)\
**Post date:** [April 4, 2021, 3:02am UTC](https://answers.netlify.com/t/accessing-file-from-function-deployment/35370/4 "2021-04-04T03:02:24Z")

</div>

Actually, scratch that… 🥶  
Looks like it worked on my local dev environment, but not working on prod.  
I am getting error message  
`[Error: ENOENT: no such file or directory, open '/var/task/src/functions/test.pdf']`

Before this I am assigning and logging this and it seems to work on prod:  
let contents = path.resolve(\_\_dirname + “/test.pdf”);  
console.log(path.resolve(contents));

---

<div class="post-metadata">

**Author:** ![Steven](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/steven/32/23695_2.png) [@Steven](https://answers.netlify.com/u/Steven)\
**Post date:** [April 4, 2021, 9:25pm UTC](https://answers.netlify.com/t/accessing-file-from-function-deployment/35370/5 "2021-04-04T21:25:21Z")

</div>

I found another post with a similar issue:

> [@Referencing files in functions](https://answers.netlify.com/t/referencing-files-in-functions/30974/3):
>
> 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?

I’ve tried to imitate the solution by requiring it but it returns an empty object.

const pdfFile = require(“./test.pdf”);  
console.log(pdfFile); //{}

---

<div class="post-metadata">

**Author:** ![Steven](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/steven/32/23695_2.png) [@Steven](https://answers.netlify.com/u/Steven)\
**Post date:** [April 6, 2021, 6:37pm UTC](https://answers.netlify.com/t/accessing-file-from-function-deployment/35370/6 "2021-04-06T18:37:09Z")

</div>

Found the solution. Looks like functions within subdirectories are bundled with others within. I tried this initially but wouldn’t build the sub-directory functions, so I thought this wasn’t the solution. The solution was to name the subfolder the same as the function name. functions/myfunc/myfunc.js

---

<div class="post-metadata">

**Author:** ![perry](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/perry/32/16_2.png) [@perry](https://answers.netlify.com/u/perry)\
**Post date:** [April 6, 2021, 6:40pm UTC](https://answers.netlify.com/t/accessing-file-from-function-deployment/35370/7 "2021-04-06T18:40:51Z")

</div>

thank you for sharing, steven!
