How to access files from NextJS API Function

Hello!

I’m working on an app that’ll send emails when a form is submitted. The email is generated via MDX using a NextJS API function, which gets converted into a Netlify Function on deploy. The NextJS API function does something like this:

const root = process.cwd();
const files = fs.readdirSync(path.join(root, 'data', type));

where type="books".

which works locally. On Netlify, I get this error:

{
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "Error: ENOENT: no such file or directory, scandir '/var/task/data/books'",
"trace": [
  "Runtime.UnhandledPromiseRejection: Error: ENOENT: no such file or directory, scandir '/var/task/data/books'",
  "    at process.<anonymous> (/var/runtime/index.js:35:15)",
  "    at process.emit (events.js:314:20)",
  "    at processPromiseRejections (internal/process/promises.js:209:33)",
  "    at processTicksAndRejections (internal/process/task_queues.js:98:32)"
]
}

Here’s a simplified file structure:

.
├── data
│   └── books
│       ├── bharatiya-sangeet-vadya.mdx
│       ├── ome-swarlipi-mein-misrabani.mdx
│       └── sur-vahini.mdx
└── pages
    ├── _app.js
    ├── api
    │   └── checkout.js
    ├── book
    │   └── [slug].js
    ├── cart.js
    └── index.js

With the calling code in pages/api/checkout.js, which is trying to read data/books/xyz.mdx.

This works on my local, using next build and next start. I assume it’ll work in Vercel as well. It is failing on Netlify because it changes the functions under the hood. Do these functions not have access to the repo files? And if so, how can I get them working?

Thanks.

To see the error for yourself, try the following steps:

That’s correct, the function does not have access to the data folder. You could make this work by generating a json with all the data and then fetch that from the function.

2 Likes

hello! fyi we now offer support for including local files in your runtime env (aka api functions). check it out here! File-based configuration | Netlify Docs (relevant part is included_files)

2 Likes