Skip to content

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

@LeRoiLambda

Description

@LeRoiLambda

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions