-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mongoose does not work with latest Next.js version (14.0.4 > 14.0.5-canary.14) #14183
Comments
Hello @edxeth, I had the same error and found your issue when trying to resolve mine. I went back en forth between my commits to see what change caused this issue. It was after a change to my package.json and package-lock.json that this bug first occured. I removed the package-lock.json and then ran npm install again to create a new package-lock.json and the bug does not occur anymore. Unfortunately I could not find what causes the issue since we use many packages in our project. We are using Next version 14.0.4-canary.32 (bug I guess that 14.0.4 should also work). Hopefully this will fix your issue as well! |
Hey there! |
@twan-keizer I thought it's worth a try so I've decided to install Next.js 14.0.4-canary.32 and guess what? It's working! Edit: the latest working version with Mongoose is 14.0.4-canary.37, for your information. |
Hi @IslandRhythms |
Where you describe the route to ping |
The route is correctly described as What am I missing here? I'm confused. I'm able to GET request the route, no problem whatsoever. Switching Next.js versions, with the same exact Mongoose implementation as shown here, causes Mongoose to throw the error I shared in my original post. |
@edxeth can you please create a GitHub repo that demonstrates the issue you're seeing? We're testing with https://github.com/IslandRhythms/demo-next-error and everything seems to work fine. |
@vkarpov15 I've just given the same exact Mongoose implementation a try in a fresh Next.js project (versions 14.0.4 and 14.0.5-canary.23), and it's working smoothly (why I didn't try this earlier?). However, in the codebase I'm currently working on, it only functions without any issues when I use Next.js versions 14.0.4-canary.32 and 14.0.4-canary.37. Anything else seems to trigger the error I mentioned in my original post. Unfortunately, I can't share the repository since it contains proprietary code. So, I'll dig into it further to identify the problem myself. Perhaps it's related to a specific package or configuration. Thanks once again for your assistance, and in the meantime, I'll stick to using Next.js 14.0.4-canary.37 |
@edxeth let us know if you find anything more. Are you at least able to share the project's |
@vkarpov15 my package.json: {
"name": "protocol",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev --turbo",
"build": "next build",
"start": "next start",
"lint": "next lint",
"migrate:init": "migrate-mongo init -m esm",
"migrate:status": "migrate-mongo status -f './mongodb.config.mjs'",
"migrate:create": "migrate-mongo create -f './mongodb.config.mjs'",
"migrate:up": "migrate-mongo up -f './mongodb.config.mjs'",
"migrate:down": "migrate-mongo down -f './mongodb.config.mjs'"
},
"dependencies": {
"@fortawesome/fontawesome-free": "6.5.1",
"@fortawesome/fontawesome-svg-core": "6.5.1",
"@fortawesome/free-brands-svg-icons": "6.5.1",
"@fortawesome/free-solid-svg-icons": "6.5.1",
"@fortawesome/react-fontawesome": "0.2.0",
"@headlessui/react": "1.7.17",
"@heroicons/react": "2.0.18",
"@rainbow-me/rainbowkit": "1.3.2",
"@rainbow-me/rainbowkit-siwe-next-auth": "0.3.4",
"@reduxjs/toolkit": "2.0.1",
"@tailwindcss/forms": "0.5.7",
"autoprefixer": "10.4.16",
"daisyui": "4.4.24",
"ethers": "6.9.1",
"migrate-mongo": "11.0.0",
"mongodb": "6.3.0",
"mongoose": "8.0.3",
"next": "14.0.4-canary.37",
"next-auth": "4.24.5",
"postcss": "8.4.32",
"prettier": "3.1.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-redux": "9.0.4",
"redux": "5.0.1",
"siwe": "2.1.4",
"tailwindcss": "3.4.0",
"viem": "1.21.1",
"wagmi": "1.4.12"
},
"devDependencies": {
"@types/node": "20.10.5",
"@types/react": "18.2.45",
"@types/react-dom": "18.2.18",
"@typescript-eslint/eslint-plugin": "6.16.0",
"eslint": "8.56.0",
"eslint-config-next": "14.0.4",
"ts-node": "10.9.2",
"typescript": "5.3.3"
},
"resolutions": {
"whatwg-url": "13.0.0"
}
} Do you mind trying with yarn and bun package managers? |
Hey thanks for opening this thread, it was quite helpful. I was experiencing the same issue and error message with After updating to
|
Hey there @dshaby |
hello I think I have similar issue here. I also got but in my case I got
"dependencies": {
"mongoose": "^8.0.3",
"react": "^18",
"react-dom": "^18",
... and other package I think not cause this error.
},
"devDependencies": {
"next": "^14.0.4",
"typescript": "^5"
... and other package I think not cause this error.
}
export async function GET(_: NextRequest, { params: { type } }: params) {
let disconnect;
try {
const conn = mongoose.createConnection(`${process.env.MONGO_URL}/basic-setting`)
// three schema start with DB_ is import from schema file
const member = conn.model('members', DB_basicSetting_member)
const category = conn.model('categories', DB_basicSetting_category)
const tag = conn.model('tags', DB_basicSetting_tag)
disconnect = conn.close
switch (type) {
case 'member':
const member_res = await member.find().sort({ threshold: 1 }).lean()
return NextResponse.json(member_res, { status: 200 })
case "category":
const categories_res = await category.find().lean()
return NextResponse.json(categories_res, { status: 200 })
case 'tag':
const tag_res = await tag.find().lean()
return NextResponse.json(tag_res, { status: 200 })
default:
return NextResponse.json('type error', { status: 404 })
}
} catch (error) {
console.log(error);
return NextResponse.json(error, { status: 500 })
} finally {
if (disconnect) {
await disconnect()
}
}
}
export default function SettingPage() {
return (
<section className="px-8">
<h2 className="text-center text-xl uppercase">基本設定</h2>
<div className="grid gap-2">
<MemberSetting /> // this one call fetch("/api/mongoDB/basicSetting/member");
<CategorySetting /> // this one call fetch("/api/mongoDB/basicSetting/category");
<TagsSetting /> // this one call fetch("/api/mongoDB/basicSetting/tag");
</div>
</section>
);
} and I got this when I run dev and load my page
I'm new in mongoose about two weeks. so maybe is some typing error in my code. |
well I solve my problem but I don't know why I create a function call connectDB function connectDB(dbName: "basic-setting") {
const conn = createConnection(`${process.env.MONGO_URL}`, {
dbName
})
conn.once('connected', () => console.log(`connect ${dbName} success`))
conn.once('close', () => console.log(`close ${dbName}`))
conn.model("members", DB_basicSetting_member_schema)
conn.model("categories", DB_basicSetting_category_schema)
conn.model("tags", DB_basicSetting_tag_schema)
return conn
} and async function GET1 for testing export async function GET1(req: NextRequest, { params: { type } }: params) {
const conn = connectDB('basic-setting')
const DB_basicSetting_member = conn.models.members
const DB_basicSetting_category = conn.models.categories
const DB_basicSetting_tag = conn.models.tags
try {
switch (type) {
case 'member':
const member_res = await DB_basicSetting_member.find().sort({ threshold: 1 }).lean()
return NextResponse.json(member_res, { status: 200 })
case "category":
const categories_res = await DB_basicSetting_category.find().lean()
return NextResponse.json(categories_res, { status: 200 })
case 'tag':
const tag_res = await DB_basicSetting_tag.find().lean()
return NextResponse.json(tag_res, { status: 200 })
default:
return NextResponse.json('type error', { status: 404 })
}
} catch (error) {
console.log(error);
return NextResponse.json(error, { status: 500 })
} finally {
await conn.close()
}
} and async function GET2 for testing export async function GET2(req: NextRequest, { params: { type } }: params) {
// first try
const conn = connectDB('basic-setting')
const DB_basicSetting_member = conn.models.members
const DB_basicSetting_category = conn.models.categories
const DB_basicSetting_tag = conn.models.tags
const disconnect = conn.close
// second try
const { models: {
members: DB_basicSetting_member,
categories: DB_basicSetting_category,
tags: DB_basicSetting_tag
}, close: disconnect } = connectDB('basic-setting')
// both try not working
try {
switch (type) {
case 'member':
const member_res = await DB_basicSetting_member.find().sort({ threshold: 1 }).lean()
return NextResponse.json(member_res, { status: 200 })
case "category":
const categories_res = await DB_basicSetting_category.find().lean()
return NextResponse.json(categories_res, { status: 200 })
case 'tag':
const tag_res = await DB_basicSetting_tag.find().lean()
return NextResponse.json(tag_res, { status: 200 })
default:
return NextResponse.json('type error', { status: 404 })
}
} catch (error) {
console.log(error);
return NextResponse.json(error, { status: 500 })
} finally {
await disconnect()
}
} GET1 work success, but not GET2 the only different is I const conn.close as disconnect and call disconnect instead of call original conn.close. then I try this on GET2 second try const { models: {
members: DB_basicSetting_member,
categories: DB_basicSetting_category,
tags: DB_basicSetting_tag
}, ...other } = connectDB('basic-setting')
console.log(other.close); // error show here and I got some type error
I have no idea why this happen |
@WinnieS0728 your issue is unrelated: purely due to incorrect JavaScript. |
ok thank you |
Sure @edxeth. I've pasted my dependencies below.
Hope this helps! |
@edxeth I'm still using 14.0.4-canary.37 to make I could resolve the punycode deprecation warning by using node v20.0.10, so this had nothing to do with the issue. |
@twan-keizer Thanks for that. Did you mean |
@dshaby yes it's a typo, version 20.10.0 |
Seeing the same issue as OP. We're on an older version of Mongoose (6.12.3) and this only happens with Turbopack. Using Next.js with Webpack works fine. More info: if I With Webpack:
And with Turbopack:
|
@jmikrut that output is strange, makes it look like with Turbopack you're getting just Mongoose's I tried to come up with a repro based on your instructions, but I'm unable to repro. Here's the GitHub repo I put together. Mongoose v6.12.3 and 1 API endpoint that just loads a document from Mongoose. Can you please take a look and see if you can modify this repo to trigger the issue you're seeing? Based on the output you pasted I suspected it might be an OSX vs Linux issue due to case insensitive filenames in OSX, but that doesn't seem to be the case, I'm able to hit |
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
This issue was closed because it has been inactive for 19 days and has been marked as stale. |
Prerequisites
Mongoose version
8.0.3
Node.js version
21.4.0
MongoDB server version
7.0.4
Typescript version (if applicable)
5.3.2
Description
Mongoose throws error on connection.
Affected versions:
^14.0.4
Not affected versions
13.5.6
,14.0.1
,14.0.2
,14.0.3
Steps to Reproduce
Install Next.js (latest & affected: 14.0.4) with TypeScript and App Router
https://nextjs.org/docs/getting-started/installation
.env
mongodb.d.ts
lib/mongodb.ts
app/api/mongodb-test/route.ts
Send an HTTP GET request to: http://localhost:3000/api/mongodb-test
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: