sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
3.93k stars 592 forks source link

⨯ node_modules\mysql2\lib\connection_config.js (261:0) @ <unknown> ⨯ URL is not a constructor #2537

Open VerredeTrop opened 1 month ago

VerredeTrop commented 1 month ago

Hello, first, I want to express gratitude for any assistance you can provide.

As Prisma isn't compatible with Edge, I aimed to directly integrate mysql2 adapter of lucia into my authentication file. Here's the code snippet:

import { cookies } from "next/headers"
import { Lucia } from "lucia"
import { Mysql2Adapter } from "@lucia-auth/adapter-mysql"
import mysql from "mysql2/promise"

const pool = mysql.createPool(process.env.DB_URL)

const adapter = new Mysql2Adapter(pool, {
    user: "users",
    session: "sessions",
})

export const lucia = new Lucia(adapter, {
    sessionCookie: {
        expires: false,
        attributes: {
            secure: process.env.NODE_ENV === "production",
        },
    },
    getUserAttributes: (attributes) => {
        return {
            email: attributes.email,
        }
    },
})

export const validateRequest = async () => {
    const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null
    if (!sessionId) {
        return {
            user: null,
            session: null,
        }
    }

    const result = await lucia.validateSession(sessionId)
    try {
        if (result.session && result.session.fresh) {
            const sessionCookie = lucia.createSessionCookie(result.session.id)
            cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes)
        }
        if (!result.session) {
            const sessionCookie = lucia.createBlankSessionCookie()
            cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes)
        }
    } catch {}
    return result
}

I'm using Node.js 20 and Next.js 14 with the following dependencies:

"dependencies": {
    "@lucia-auth/adapter-mysql": "^3.0.2",
    "@lucia-auth/adapter-prisma": "^4.0.1",
    "@prisma/client": "^5.11.0",
    "@react-email/components": "0.0.14",
    "bcrypt": "^5.1.1",
    "bcryptjs": "^2.4.3",
    "esbuild": "^0.20.1",
    "gray-matter": "^4.0.3",
    "lucia": "^3.1.1",
    "mysql2": "^3.9.3",
    "next": "^14.1.4",
    "next-mdx-remote": "^4.4.1",
    "oslo": "^1.1.3",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-email": "2.0.0",
    "react-hot-toast": "^2.4.1",
    "react-select": "^5.8.0",
    "reading-time": "^1.5.0",
    "resend": "^3.2.0",
    "sharp": "^0.33.2",
    "uuid": "^9.0.1",
    "zod": "^3.22.4"
  },
  "devDependencies": {
    "@svgr/webpack": "^8.1.0",
    "autoprefixer": "^10.0.1",
    "eslint": "^8",
    "eslint-config-next": "^14.1.4",
    "postcss": "^8",
    "prettier": "^3.2.5",
    "prisma": "^5.11.0",
    "tailwindcss": "^3.4.1"

I encountered an error, as mentioned in the title:

image

And in the console: node_modules\mysql2\lib\connection_config.js (261:0) @ ⨯ URL is not a constructor

Subsequently, I traced the path to connection_config.js and attempted to remove line number 10 and .next folder. It seems to have resolved the issue. Could you please explain if I have made any mistakes? Thank you.

image

sidorares commented 1 month ago

This is likely a similar issue to what others experience with LRU module ( https://github.com/sidorares/node-mysql2/issues/2001 and https://github.com/sidorares/node-mysql2/issues/1885 ) and probably some edge case between the bundler and commonjs. The issue itself is likely external to this module but I'm keen to find a solution that we can recommend to others.

Could you try to make a repro repo @VerredeTrop ? Ideally as small as possible, if the problem is manifested in just next + mysql2 as a dependency that would be ideal

VerredeTrop commented 1 month ago

@sidorares You were right! This is an issue linked to Lucia. I was trying to use it in the middleware of nextjs in the quest of seeking a good package that handle session.

Thank you for your help