Next.js Discord

Discord Forum

does anyone know fixed error "module not found 'fs'" when use nodemailer in nextJS with NextAuth?

Answered
frezyn posted this in #help-forum
Open in Discord
Error:
Module not found: Can't resolve 'fs'


this error is generated when I use the "email" provider of nextAuth's nodemailer, to use magicLink.
export const { handlers, signIn, signOut, auth } = NextAuth({
  providers: [EmailProvicer], //when i remove this provider, the error disappears.
  session: {
    strategy: "database",
  },
...
}


email.provider
export const EmailProvicer =  EmailProvider({
  server: {
    host: env.EMAIL_SERVER_HOST,
    port: parseFloat(env.EMAIL_SERVER_PORT),
    auth: {
      user: env.SERVER_USER,
      pass: env.SERVER_PASSWORD,
    },
  },
  from: env.EMAIL_FROM,
  sendVerificationRequest
})



This error is caused by accessing the code on the client side, when it was only supposed to run on the server-side, but I can't find a solution for this. Does anyone know how to solve this?

obs:
One thing I discovered is that if I declare nextAuth in an API route and export it, the provider works, but it's terrible to reuse in the application, the auth methods, etc... so I wanted a way to make sure that the emailProvider will be executed on the serverSide.
Answered by frezyn
I fixed it, this error was caused because I was using the server side function on the client side. I changed auth to useSession on the client side, and it solved this error! HAHAHAHA
View full answer

1 Reply

I fixed it, this error was caused because I was using the server side function on the client side. I changed auth to useSession on the client side, and it solved this error! HAHAHAHA
Answer