sidorares / node-mysql2

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

[nuxt] [request error] [unhandled] [500] LRU is not a constructor #2001

Open Beipy opened 1 year ago

Beipy commented 1 year ago

I was able to run it during normal development, but got an error after the build package.

image

package.json The file configuration is as follows:

{
  "name": "nuxt-app",
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev --dotenv .env.local",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@types/node": "^18",
    "nuxt": "^3.4.2",
    "nuxt-windicss": "^2.6.1"
  },
  "dependencies": {
    "@css-render/vue3-ssr": "^0.15.12",
    "@vuemap/vue-amap": "^2.0.5",
    "animate.css": "^4.1.1",
    "base64-arraybuffer": "^1.0.2",
    "dayjs": "^1.11.7",
    "less": "^4.1.3",
    "less-loader": "^11.1.0",
    "mysql2": "^3.2.0",
    "naive-ui": "^2.34.3",
    "swiper": "^9.2.4",
    "vue-awesome-swiper": "^5.0.1"
  }
}
ShreshthTiwari commented 1 year ago

I'm also facing the same issue

- error node_modules/mysql2/lib/parsers/parser_cache.js (5:20) @ eval
- error Error [TypeError]: LRU is not a constructor
    at eval (webpack-internal:///(sc_server)/./node_modules/mysql2/lib/parsers/parser_cache.js:3:21)
    at Object.(sc_server)/./node_modules/mysql2/lib/parsers/parser_cache.js (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:1074:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./node_modules/mysql2/lib/parsers/text_parser.js:6:21)
    at Object.(sc_server)/./node_modules/mysql2/lib/parsers/text_parser.js (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:1096:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./node_modules/mysql2/lib/commands/query.js:7:23)
    at Object.(sc_server)/./node_modules/mysql2/lib/commands/query.js (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:590:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./node_modules/mysql2/lib/commands/index.js:4:15)
    at Object.(sc_server)/./node_modules/mysql2/lib/commands/index.js (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:557:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./node_modules/mysql2/lib/connection.js:24:18)
    at Object.(sc_server)/./node_modules/mysql2/lib/connection.js (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:645:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./node_modules/mysql2/index.js:3:20)
    at Object.(sc_server)/./node_modules/mysql2/index.js (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:403:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./src/modules/MySQL/connectDatabase.ts:5:64)
    at Object.(sc_server)/./src/modules/MySQL/connectDatabase.ts (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:1821:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./src/modules/MySQL/fetchData.ts:5:74)
    at Object.(sc_server)/./src/modules/MySQL/fetchData.ts (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:1832:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./src/app/api/accounts/find-username/route.ts:6:82)
    at Object.(sc_server)/./src/app/api/accounts/find-username/route.ts (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:1810:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Faccounts%2Ffind-username%2Froute&page=%2Fapi%2Faccounts%2Ffind-username%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Faccounts%2Ffind-username%2Froute.ts&appDir=%2Fhome%2Fshreshthtiwari%2FDesktop%2FPrrojects%2Fastropathshala-app%2Fsrc%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2Fhome%2Fshreshthtiwari%2FDesktop%2FPrrojects%2Fastropathshala-app&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=!:15:159)
    at Object.(sc_server)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Faccounts%2Ffind-username%2Froute&page=%2Fapi%2Faccounts%2Ffind-username%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Faccounts%2Ffind-username%2Froute.ts&appDir=%2Fhome%2Fshreshthtiwari%2FDesktop%2FPrrojects%2Fastropathshala-app%2Fsrc%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2Fhome%2Fshreshthtiwari%2FDesktop%2FPrrojects%2Fastropathshala-app&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=! (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:216:1)
    at __webpack_require__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/webpack-runtime.js:33:42)
    at __webpack_exec__ (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:2018:39)
    at /home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:2019:28
    at Object.<anonymous> (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/.next/server/app/api/accounts/find-username/route.js:2022:3)
    at Module._compile (node:internal/modules/cjs/loader:1196:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1250:10)
    at Module.load (node:internal/modules/cjs/loader:1074:32)
    at Function.Module._load (node:internal/modules/cjs/loader:909:12)
    at Module.require (node:internal/modules/cjs/loader:1098:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at requirePage (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/node_modules/next/dist/server/require.js:156:12)
    at /home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/node_modules/next/dist/server/load-components.js:68:84
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async loadComponentsImpl (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/node_modules/next/dist/server/load-components.js:68:26)
    at async DevServer.findPageComponentsImpl (/home/shreshthtiwari/Desktop/Prrojects/astropathshala-app/node_modules/next/dist/server/next-server.js:756:36) {
  digest: undefined
}
null

package.json

{
  "name": "something",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@types/crypto-js": "^4.1.1",
    "@types/geolite2": "^2.0.0",
    "@types/node": "20.1.1",
    "@types/react": "18.2.6",
    "@types/react-dom": "18.2.4",
    "@types/uuid": "^9.0.1",
    "axios": "^1.4.0",
    "axios-retry": "^3.4.0",
    "check-password-strength": "^2.0.7",
    "crypto-js": "^4.1.1",
    "email-validator": "^2.0.4",
    "eslint": "8.40.0",
    "eslint-config-next": "13.4.1",
    "geolite2": "^3.5.1",
    "is-ip": "^5.0.0",
    "maxmind": "^4.3.11",
    "mysql2": "^3.2.4",
    "next": "13.4.1",
    "nodemailer": "^6.9.1",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "typescript": "5.0.4",
    "universal-cookie": "^4.0.4",
    "uuid": "^9.0.0"
  }
}
cesartagplus commented 1 year ago

Also facing this issue, but with nuxt2

wellwelwel commented 1 year ago

@cesartagplus, are you seeing this error in the version ^3.3.1 of mysql2?

Rox-Cat commented 6 months ago

@Beipy Hello, I have encountered the same issue. How did you solve this issue?

JasonFeng03 commented 5 months ago

The reason I found is that nuxt builds convert mysql2's lru-cache dependency into a symbolic link. But I don't know how to fix it

wellwelwel commented 5 months ago

The reason I found is that nuxt builds convert mysql2's lru-cache dependency into a symbolic link. But I don't know how to fix it

@JasonFeng03, can you share a basic repro of this error?

JasonFeng03 commented 5 months ago

@wellwelwel Initialize a nuxt project Install mysql2 dependencies, create a database connection, and build the project 1

Rox-Cat commented 5 months ago

@JasonFeng03 Hi, I solved this problem by using pnpm to install dependencies. Maybe you can try pnpm instead of yarn or npm.

JasonFeng03 commented 5 months ago

enabled of nitro's experimental configurationexperimental: { legacyExternals: true },Use the old dependency algorithm to solve this problem.But the official documentation says that approach is unstable

v0id-4lpz commented 4 months ago

Same error here

After a npm run build and uploaded my files on my server and pm2 start

0|Project | You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
0|Project | TypeError: LRU$1 is not a constructor
0|Project |     at file:///home/project/server/chunks/runtime.mjs:9827:22
0|Project |     at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
0|Project |     at async Promise.all (index 0)
0|Project |     at ESMLoader.import (node:internal/modules/esm/loader:530:24)
0|Project |     at importModuleDynamicallyWrapper (node:internal/vm/module:438:15)

Here is my deps

  "devDependencies": {
    "bcryptjs": "^2.4.3",
    "drizzle-kit": "^0.20.14",
    "nuxt": "^3.10.3",
    "nuxt-security": "^1.1.0",
    "vite-plugin-vuetify": "^2.0.1",
    "vue": "^3.4.15",
    "vue-router": "^4.2.5",
    "vuetify": "^3.5.3"
  },
  "overrides": {
    "@parcel/watcher": "~2.1.0"
  },
  "dependencies": {
    "@mdi/font": "^7.4.47",
    "@sidebase/nuxt-auth": "^0.6.7",
    "drizzle-orm": "^0.29.3",
    "jsonwebtoken": "^9.0.2",
    "mailersend": "^2.2.0",
    "moment": "^2.30.1",
    "mysql2": "^3.9.2",
    "next-auth": "^4.22.5",
    "vee-validate": "^4.12.5",
    "yup": "^1.3.3"
  }
JasonFeng03 commented 3 months ago

@v0id-4lpz I solved this problem temporarily by building a script scripts: { "build": "nuxt build --dotenv .env.prod && npm run del-lru-cache && npm run copy-lru-cache", "del-lru-cache": "rm -rf .output\\server\\node_modules\\mysql2\\node_modules\\lru-cache", "copy-lru-cache": "cp -r node_modules\\mysql2\\node_modules\\lru-cache .output\\server\\node_modules\\mysql2\\node_modules" }