Next.js Discord

Discord Forum

Deploy next.js app to vercel that uses mysql2

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
I am really new to next.js but I am trying to deploy a app that has mysql2 and I keep getting this error when deploying.
Can I even use mysql2?

Im using Next.js 14, Next auth v5 and like I said mysql2
Answered by joulev
Next-auth cannot do anything about the database driver not being supported on the edge runtime unfortunately.

If you happen to use planetscale, you can use their serverless driver which is compatible with the edge runtime: https://github.com/planetscale/database-js.

If you don’t use planetscale, tough luck. You have to do one of these:

1. Find a way to use the planetscale driver for databases outside planetscale. Doesn’t look like that is possible.

2. Not use middleware at all, and implement the authentication redirection yourself in all routes.

3. Migrate to planetscale (they don’t have a free plan) or neon (they have a free plan, but postgresql not mysql) because they have drivers supporting the edge runtime. Other database providers work too as long as they have an edge compatible way to query their databases.
View full answer

16 Replies

Sun bearOP
If you need a better view of the errors
@joulev You can’t use mysql2 in the edge runtime. Use nodejs instead
Sun bearOP
Thank you for the answer! Can you explain me how do I do that?
@Sun bear Thank you for the answer! Can you explain me how do I do that?
you have at least one route using mysql2 that is run on the edge runtime. simply switch to the nodejs runtime instead.
Sun bearOP
Ok, but what do I have to change in my files I’m not understanding. Is it a config file?
@Sun bear Ok, but what do I have to change in my files I’m not understanding. Is it a config file?
Hmmmm. Are you importing the src/auth.ts file into middleware?
Outside middleware, the edge runtime is opt in, so you simply remove the opt in declaration. In middleware you cannot opt out of the edge runtime so you cannot run mysql2 there
Sun bearOP
I’m in mobile right now
This is what I have in middleware
And it’s supposed to be like this no?
@Sun bear The thing is, I’m using Next Auth V5
Next-auth cannot do anything about the database driver not being supported on the edge runtime unfortunately.

If you happen to use planetscale, you can use their serverless driver which is compatible with the edge runtime: https://github.com/planetscale/database-js.

If you don’t use planetscale, tough luck. You have to do one of these:

1. Find a way to use the planetscale driver for databases outside planetscale. Doesn’t look like that is possible.

2. Not use middleware at all, and implement the authentication redirection yourself in all routes.

3. Migrate to planetscale (they don’t have a free plan) or neon (they have a free plan, but postgresql not mysql) because they have drivers supporting the edge runtime. Other database providers work too as long as they have an edge compatible way to query their databases.
Answer
@Sun bear So the problem is that I’m using mysql2 I can convert the queries and the other stuff to another database provider, can you recommend me the databases providers that works the best and are free?
Edge compatible database drivers require certain things to be done in the server where the database is hosted. So no, you can’t simply take a driver supporting the edge runtime to connect to your own db and expect it to work. Your database has to be in a place with the necessary infrastructure. Neon is the only place I know of that has a free plan and supports this