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
Comments
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 |
@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. |
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. You can avoid mongoose error by checking models to see if let modelName = ...
let schema = ...
const Model = mongoose.models[modelName] || mongoose.model(modelName, schema);
export default Model; |
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
@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 |
Yes, you are right.
Do you mean to hardcode mongoose and other dependencies? |
@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). |
@farnabaz That could be a quick nice patch by modifying regex
@kamran-12 Using |
@pi0 if I understood right, you suggested to replace
That would be helpful. |
fixed by v2.14.4 @kamran-12 now you should be able to normally use serverMiddleware without HMR issues with mongoose :) |
@liho00 Would you open a new issue with a reproduction? 🙏 |
Why this has been closed? It still persists in nuxt 2.15.7 |
@AregGhazaryan If it's still persisting for you, would you open a new issue with a reproduction? 🙏 |
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? |
Versions
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.
The text was updated successfully, but these errors were encountered: