question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dependency loading issue in canary with Experimental API Support

See original GitHub issue

I am trying out the canary build with the experimental API support https://github.com/zeit/next.js/pull/7296.

I am using Mongoose as my ORM and when I try load a schema from the /pages/api/product.js like

const Category = require('../../models/category')
export default async (req,res) => {
  const data = Category.find{...}
 res.json(data)
}

I am getting this error


{ OverwriteModelError: Cannot overwrite `Category` model once compiled.
    at new OverwriteModelError (/Users/rameshvel/work/dailybasket/node_modules/mongoose/lib/error/overwriteModel.js:20:11)
    at Mongoose.model (/Users/rameshvel/work/dailybasket/node_modules/mongoose/lib/index.js:423:13)
    at Object../models/category.js (/Users/rameshvel/work/dailybasket/.next/server/static/development/pages/api/category.js:142:27)
    at __webpack_require__ (/Users/rameshvel/work/dailybasket/.next/server/static/development/pages/api/category.js:23:31)
    at Module../pages/api/category.js (/Users/rameshvel/work/dailybasket/.next/server/static/development/pages/api/category.js:498:16)
    at __webpack_require__ (/Users/rameshvel/work/dailybasket/.next/server/static/development/pages/api/category.js:23:31)
    at Object.3 (/Users/rameshvel/work/dailybasket/.next/server/static/development/pages/api/category.js:605:18)
    at __webpack_require__ (/Users/rameshvel/work/dailybasket/.next/server/static/development/pages/api/category.js:23:31)
    at /Users/rameshvel/work/dailybasket/.next/server/static/development/pages/api/category.js:91:18
    at Object.<anonymous> (/Users/rameshvel/work/dailybasket/.next/server/static/development/pages/api/category.js:94:10)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at DevServer.handleApiRequest (/Users/rameshvel/work/dailybasket/node_modules/next-server/dist/server/next-server.js:180:59)
  message: 'Cannot overwrite `Category` model once compiled.',
  name: 'OverwriteModelError' }
[ event ] disposing inactive page(s): /api/category
[ ready ] compiled successfully

If I comment out the mongoose model loading and just return any random json it works fine.

System information

  • OS: macOS
  • Version of Next.js: [Latest Canary]

Seems something is forcing to recreate the mongoose model. any ideas?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
johnbarhorstcommented, Aug 16, 2020

I ran into this same issue today, with next v- 9.5.2. New to next, it took me a bit to stumble on this solution. Ultimately it looks like making sure I’m consistent with using import statements, instead of copy paste my old require syntax from old mongo files fixed a lot of things. In case anyone else has a head v wall day like I did today. Also an npm rebuild was thrown in with a server restart.

1reaction
joshleblanccommented, May 20, 2019

Instead of exporting the mongoose.model(‘Category’, schema), try requiring the file and using mongoose.Model('Category') to use the model. It kind of seems like next is running the import twice and trying to re-create the Category model.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency loading issue in canary with Experimental API ...
I am trying out the canary build with the experimental API support #7296. I am using Mongoose as my ORM and when I...
Read more >
Library functions available for Node.js canary scripts
Explains the built-in functions included in CloudWatch Synthetics that you can use to write your own canary scripts in Node.js.
Read more >
Canary Deployments - GitLab Docs
Configure a canary deployment job for Auto DevOps pipelines. ... environment's deploy board by using GraphiQL, or by sending requests to the GraphQL...
Read more >
Change Log - LeakCanary - Square Open Source
I built LeakCanary to help fix leaks, but in doing so I accidentally wrote a fairly flexible heap dump parser. Since we're parsing...
Read more >
Android dependency has different version for the compile and ...
The only thing that using api over implementation does is to expose your library dependencies to the app project even if the app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found