OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I solve Error after deploying on vercel?

  • Thread starter Thread starter Tabish khan
  • Start date Start date
T

Tabish khan

Guest
I have hosted my portfolio website using vercel and when I am trying to send mail I am getting this error

Code:
Error: Error: ENOENT: no such file or directory, open 'src/app/emailcount.json'
    at async open (node:internal/fs/promises:636:25)
    at async Object.readFile (node:internal/fs/promises:1246:14)
    at async n6 (/var/task/.next/server/app/api/sendmail/route.js:4:88894)
    at async /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:36258
    at async eR.execute (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:26874)
    at async eR.handle (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:37512)
    at async es (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:25465)
    at async en.responseCache.get.routeKind (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:1026)
    at async r6.renderToResponseWithComponentsImpl (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:508)
    at async r6.renderPageComponent (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:5121) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'src/app/emailcount.json'
}

here is the code of my function

Code:
import EmailTemplate from "@/app/components/EmailTemplate";
import { NextRequest, NextResponse } from "next/server";
import { Resend } from "resend";
import { promises as fs } from "fs";

const resend = new Resend(process.env.RESEND_API_KEY);

export async function POST(req: NextRequest) {
  try {
    const file = await fs.readFile("src/app/emailcount.json",
      "utf8"
    );
    const emailcount = JSON.parse(file);

    const { name, email, phonenumber, emailsub, message }: any = await req.json();
    console.log("name is:", name);

    const test = "portfolio" + emailcount.count;
    console.log(test);

    const data = await resend.emails.send({
      from: `${name} <${test}@resend.dev>`,
      to: "[email protected]",
      subject: emailsub,
      react: EmailTemplate({
        name: name,
        phonenumber: phonenumber,
        email: email,
        emailsub: emailsub,
        message: message,
      }),
    });

    console.log("data is:", data.data?.id);

    // Update the email count after a successful send
    emailcount.count += 1;
    await fs.writeFile( "src/app/emailcount.json",
      JSON.stringify(emailcount, null, 2),
      "utf8"
    );

    return NextResponse.json({
      data,
      message: "Email sent successfully",
      success: true,
      status: 200,
    });
  } catch (error) {
    console.error("Error:", error);
    return NextResponse.json({
      error,
      message: "Failed to send email",
      success: false,
      status: 500,
    });
  }
}

enter image description here


and this is file structure

It is working properly in local enviroment that is localhost but after deploying It does not work

I have tried changing paths in different ways

"../../countemail.json" "src/app/countemail.json"

<p>I have hosted my portfolio website using vercel and when I am trying to send mail I am getting this error</p>
<pre><code>Error: Error: ENOENT: no such file or directory, open 'src/app/emailcount.json'
at async open (node:internal/fs/promises:636:25)
at async Object.readFile (node:internal/fs/promises:1246:14)
at async n6 (/var/task/.next/server/app/api/sendmail/route.js:4:88894)
at async /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:36258
at async eR.execute (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:26874)
at async eR.handle (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:37512)
at async es (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:25465)
at async en.responseCache.get.routeKind (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:1026)
at async r6.renderToResponseWithComponentsImpl (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:508)
at async r6.renderPageComponent (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:5121) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'src/app/emailcount.json'
}
</code></pre>
<p>here is the code of my function</p>
<pre><code>import EmailTemplate from "@/app/components/EmailTemplate";
import { NextRequest, NextResponse } from "next/server";
import { Resend } from "resend";
import { promises as fs } from "fs";

const resend = new Resend(process.env.RESEND_API_KEY);

export async function POST(req: NextRequest) {
try {
const file = await fs.readFile("src/app/emailcount.json",
"utf8"
);
const emailcount = JSON.parse(file);

const { name, email, phonenumber, emailsub, message }: any = await req.json();
console.log("name is:", name);

const test = "portfolio" + emailcount.count;
console.log(test);

const data = await resend.emails.send({
from: `${name} <${test}@resend.dev>`,
to: "[email protected]",
subject: emailsub,
react: EmailTemplate({
name: name,
phonenumber: phonenumber,
email: email,
emailsub: emailsub,
message: message,
}),
});

console.log("data is:", data.data?.id);

// Update the email count after a successful send
emailcount.count += 1;
await fs.writeFile( "src/app/emailcount.json",
JSON.stringify(emailcount, null, 2),
"utf8"
);

return NextResponse.json({
data,
message: "Email sent successfully",
success: true,
status: 200,
});
} catch (error) {
console.error("Error:", error);
return NextResponse.json({
error,
message: "Failed to send email",
success: false,
status: 500,
});
}
}

</code></pre>
<p><img src="https://i.sstatic.net/lQNJAhy9.png" alt="enter image description here" /></p>
<p>and this is file structure</p>
<p>It is working properly in local enviroment that is localhost but after deploying It does not work</p>
<p>I have tried changing paths in different ways</p>
<p>"../../countemail.json"
"src/app/countemail.json"</p>
 
Top