Navigation Menu

Skip to content
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

App crashes on ServerMiddleware hot reload if Mongoose models are used, gives an error like "ServerMiddleware Error: Cannot overwrite User model once compiled" #7917

Closed
kamran-12 opened this issue Aug 14, 2020 · 15 comments · Fixed by #7938

Comments

@kamran-12
Copy link

kamran-12 commented Aug 14, 2020

Versions

  • nuxt: v2.14.1
  • node: v12.18.3

Reproduction

Use this project to reproduce, you may need to add a working MongoDB database credentials in .env file.

Steps to reproduce

npm install
npm run dev
(No error at this point yet)
Make a change in login.js file
Save

What is Expected?

Normal hot reload of servermiddleware as always (like when Mongoose is not used).

What is actually happening?

App crashes with this error:

ERROR ServerMiddleware Error: Cannot overwrite User model once compiled.

@farnabaz
Copy link
Member

This isn't related to Nuxt, This is mongoose error. When a change occurs and Nuxt reload model files and mongoose models call twice.

Here you can find a fix: https://stackoverflow.com/questions/19051041/cannot-overwrite-model-once-compiled-mongoose

@kamran-12
Copy link
Author

kamran-12 commented Aug 14, 2020

@farnabaz This is actually a problem with how reloads are implemented with nuxt. I looked at the stackoverflow example you gave me before starting to write this bug report, and although error is same, reasons for it are different.

In stackoverflow example: person with name Anathema.Imbued made a mistake by defining the same model twice within the project, got error immediately (not just on reload).

In my example: didn't do such mistake, same mongoose code works well outside nuxt, also works well inside nuxt until reload. Crashes only during hot reload of server middleware.

Hot reloads of server middleware should be done exactly like nodemon.

Please re-open.

@farnabaz
Copy link
Member

Nodemon restarts the whole server on file change, but Nuxt removes the cache and re-import the file. I believe this is not a bug its nuxt approach to reload files.
Restarting the server on every file change has a great impact on performance.

You can avoid mongoose error by checking models to see if User model was registered or not.

let modelName = ...
let schema = ...
const Model = mongoose.models[modelName] || mongoose.model(modelName, schema);

export default Model;

@pi0 pi0 reopened this Aug 14, 2020
@pi0
Copy link
Member

pi0 commented Aug 14, 2020

This issue is both with nuxt HMR and mongoose. Using default connection, it keeps instance in CJS cache and when reloading app, it causes this error

You can avoid mongoose error by checking models to see if User model was registered or not.

@farnabaz I guess this will cause not applying schema changes.

One workaround is doing full reload for server by directly importing serveriMiddleware like this:

export default {
  serverMiddleware: [
    require('./api')
  ]
}

Maybe we can also introduce an option to HMR some node_module dependencies. Currently we don't clear cache for all node_modules (src) which is also not configurable but if it is, we can specify mongoose or other HMR incompatible dependencies.

@farnabaz
Copy link
Member

farnabaz commented Aug 14, 2020

I guess this will cause not applying schema changes.

Yes, you are right.

we can specify mongoose or other HMR incompatible dependencies.

Do you mean to hardcode mongoose and other dependencies?

@pi0

@kamran-12
Copy link
Author

@pi0 what can be a temporary solution for me? So that when something inside servermiddleware changes, all of the servermiddleware (but not the whole nuxt server) restarts (like nodemon)? That would be good enough for me (as nodemon restart takes ~1 sec., while full nuxt restart takes 5 to 10 sec (that needs improvement, too, btw)). I guess what you suggested was supposed to be what I am asking for right now, but it does not work (the same error is still there).

@pi0
Copy link
Member

pi0 commented Aug 17, 2020

Do you mean to hardcode mongoose and other dependencies?

@farnabaz That could be a quick nice patch by modifying regex

What can be a temporary solution for me?

@kamran-12 Using require as mentioned above, will cause full restart. HMR is to only replace some requires on runtime to avoid full restart. Alternatively before fix for mongoose, you can use concurrently to run API standalone and make requests from UI to it. (can provide more detailed info how to do)

@kamran-12
Copy link
Author

@pi0 if I understood right, you suggested to replace serverMiddleware: [{path: '/api', handler: '~/api/index'}] to serverMiddleware: [require('./api')] in nuxt.config.js. It didn't work. The error didn't go away. I have said this in the last sentence of my previous comment.

Alternatively before fix for mongoose, you can use concurrently to run API standalone and make requests from UI to it. (can provide more detailed info how to do)

That would be helpful.

@pi0 pi0 closed this as completed in #7938 Aug 26, 2020
@pi0 pi0 mentioned this issue Aug 27, 2020
@pi0
Copy link
Member

pi0 commented Aug 27, 2020

fixed by v2.14.4 @kamran-12 now you should be able to normally use serverMiddleware without HMR issues with mongoose :)

@liho00
Copy link

liho00 commented May 10, 2021

Same issue on nuxt@2.15.5
image

@danielroe
Copy link
Member

@liho00 Would you open a new issue with a reproduction? 🙏

@AregGhazaryan
Copy link

Why this has been closed? It still persists in nuxt 2.15.7

@danielroe
Copy link
Member

@AregGhazaryan If it's still persisting for you, would you open a new issue with a reproduction? 🙏

@AregGhazaryan
Copy link

@danielroe #9456

@TomBell95
Copy link

This issue still persists, waiting for a response on #9456 - have tried the workaround as per @pi0 suggested however this does not work.

Having to manually restart the Mongoose backend for my app every time there is a change in the file.

Is there any update please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants