Using Deno Deploy with Drizzle and MySQL2
Hi! I was trying to deploy a nuxt project to deno deploy today. It uses mysql2 and drizzle for the database stuff. However, after deploying it, I got this error:
It seems like nuxt uses
unenv
under-the-hood which tries to polyfill net.connect, but net.connect
specifically isn't implemented yet. The issue presumably happens when mysql2 tries to connect to my db, because it uses this package under-the-hood.
Do I have it right? And if so, how can I fix it? I tried to set clientNodeCompat
to false in the nuxt config without any success.
It's a very simple nuxt project. Basically just npx nuxi@latest init
with drizzle added to it. I would make the github repo public, but I'm not that confident I haven't leaked some credential somewhere.
2 Replies
Hi @Pixel. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the information provided in your question and the knowledge sources available, I can offer some insights and suggestions regarding your issue with Deno Deploy, Drizzle, and MySQL2 in a Nuxt project.
The error you're encountering is indeed related to the
unenv
package, which is used by Nuxt to provide polyfills for various Node.js APIs in different environments. The specific error suggests that the net.connect
function is not implemented in the current environment, which is likely Deno Deploy in this case.
You're correct in assuming that this issue occurs when MySQL2 tries to connect to your database, as it relies on Node.js's net
module for establishing connections.
Here are some potential solutions and workarounds:
1. Use a different database driver: Deno Deploy might not be fully compatible with MySQL2 due to its reliance on Node.js-specific APIs. Consider using a Deno-compatible database driver or a different database solution that works well with Deno Deploy.
2. Use Cloudflare D1 or other edge-compatible databases: If you're deploying to an edge environment like Deno Deploy, you might want to consider using databases specifically designed for edge computing, such as Cloudflare D1.
3. Use a different deployment target: If you need to use MySQL2 and Drizzle, you might need to deploy your Nuxt application to a Node.js-compatible environment instead of Deno Deploy.
4. Check Nuxt's Cloudflare Pages deployment options: Nuxt has specific configurations for Cloudflare Pages deployments, which might be relevant to your Deno Deploy setup. You can try setting the nodejs_compat
flag as mentioned in this GitHub issue comment: Link to GitHub issue comment (https://github.com/nuxt/nuxt/issues/29188#issuecomment-2023837090)
Add the following to your nuxt.config.ts
:
5. Explore Deno-compatible database solutions: Look into database solutions that are specifically designed to work with Deno, such as Deno KV. The Unstorage library, which is used by Nuxt, has a Deno KV driver that might be worth exploring: Unstorage Deno KV Driver (https://unstorage.unjs.io/drivers/deno)
Unfortunately, the knowledge sources don't provide a definitive solution for using MySQL2 and Drizzle with Deno Deploy in a Nuxt project. You might need to explore alternative database solutions or deployment targets that are more compatible with your current setup.
If you decide to stick with Deno Deploy, you may need to refactor your database access layer to use Deno-compatible APIs and drivers. This might involve significant changes to your current setup, including replacing MySQL2 and potentially Drizzle with Deno-friendly alternatives.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@161559534916206592> Kapa.ai is still learning and improving, please let me know how I did by reacting below.