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:
- Go to https://omenad-shop.netlify.app/book/sitar-compositions-in-ome-swarlipi
- Click “Add to Cart”
- Click “Cart” in the top-right
- Fill out some random details in the form
- Check the “I Agree” button and hit Submit
- See the error