Back

NextJS Middleware is running into "window is not defined" error

  • 0
  • Web
falcon
9 Mar, 2024, 18:25

I'm using the appwrite client side sdk for my NextJS project. While importing the appwrite utility functions in the middleware.js file, I'm running into the error ''window is not defined". My understanding is that in next.js, the middleware runs on the server side and then on the client. I tried to conditionally import the module only middleware runs on the client side but its not working. Can someone help here ?

TL;DR
Developers are encountering a "window is not defined" error when using the appwrite client-side SDK in a NextJS middleware file. The issue arises because NextJS middleware runs both on the server and client side. To address this, one solution is to conditionally import the module for client-side execution only.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more