Next.js Discord

Discord Forum

Deploy app to Vercel but email-template not found

Unanswered
Black Caiman posted this in #help-forum
Open in Discord
Black CaimanOP
When i want to to deploy my app to vercel all is good, but when i want to send email and get my email template i have this error :

Error: ENOENT: no such file or directory, open '/var/task/email-template/mail-verification.html'
    at Object.openSync (node:fs:596:3)
    at Object.readFileSync (node:fs:464:35)
    at d (/var/task/.next/server/chunks/655.js:1:142305)
    at h (/var/task/.next/server/chunks/655.js:1:140934)
    at async /var/task/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:406
    at async rm (/var/task/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:15:6342)
    at async rq (/var/task/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:18:1249)
    at async Y (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:25461)
    at async Q.responseCache.get.routeKind (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:1025)
    at async r2.renderToResponseWithComponentsImpl (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:507) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/var/task/email-template/mail-verification.html'
}

the folder "email-template" is at the top of the project like this :

-> project
--> email-template
--> public
--> src
--> app ...


and there is my code :

  const emailTemplate = fs.readFileSync(process.cwd() + '/email-template/mail-verification.html', 'utf-8');

  const emailHtml = emailTemplate.replace('${confirmLink}', confirmLink);

  await resend.emails.send({
    from: `${from}`,
    to: email,
    subject: "Vérification d'e-mail",
    html: emailHtml,
  });

2 Replies

Gharial
The cwd won't be the same on the prod env as local, so you'll need to resolve the right path. Moving to pubic might also work but it's hard to say without knowing more. Try this - I'm on mobile so sorry for the formatting/you'll want to double check
import path from 'path';
import fs from 'fs';


const emailTemplatePath = path.join(__dirname, '..', 'email-template', 'mail-verification.html');
const emailTemplate = fs.readFileSync(emailTemplatePath, 'utf-8');

const emailHtml = emailTemplate.replace('${confirmLink}', confirmLink);

await resend.emails.send({
  from: `${from}`,
  to: email,
  subject: "Vérification d'e-mail",
  html: emailHtml,
});
If your server-side code file is in a deeper nested directory you might need to adjust the '..' part of path.join() to correctly point to where your email-template folder is