fix: Do not interrupt the module setup process when missing MONGODB_URI #54
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TL;DR
An early return statement in the setup function of
nuxt-mongoose
module interrupts type-generation process due to missing envMONGODB_URI
at build process and cause error at runtime.Related Issue
#52
Background Information
I integrated mongoose into my Nuxt3 project using the nuxt-mongoose module. During the development and deployment process, I discovered the following problems:
npm run dev
,npm run build
,npm run preview
works perfect.npm install
thennpm run build
pm2 start pm2.config.js
which I inject my environment variables in INCLUDING theMONGODB_URI
Note: The
npm run dev
,npm run build
andnpm run preview
mentioned above refer tonuxt dev
,nuxt build
andnuxt preview
respectively.What the bug looks like
Weird things happened during both the build process and runtime
build process
During the build process running on the builder-machine of Github Actions, more specifically AFTER building the client bundle and BEFORE building the server bundle.
runtime
One of my API routes use the schema to fetch data from the remote database, the following error occurs.
Reproduction
I've made a minimal reproduction here. The reproduction steps are written in
README.md
inside the following StackBlitz project πnuxt-mongoose PR issue#52 reproduction - StackBlitz
How the bug happens
nuxt-mongoose
will first merge the user's config and the default config, and then generate the typing file.nuxt/types/nuxt-mongoose.d.ts
which containsdeclare module '#nuxt/mongoose' {... ..}
, this setup function will be executed at BUILD time.MONGODB_URI
env during the BUILD PROCESS(e.g. enject envs via PM2 config), the setup process will be interrupted by an early return, and the subsequent statements which generates type files located in.nuxt/
would not be executed, causing the@rollup/plugin-node-resolve
of Rollup not recognizing'#nuxt/mongose'
during compilation thus treat it as an external dependency, causing this line of statement to be retained in the compiled product, triggering an error at runtime.Conclusion
I think we should not interrupt the type-generation process when
MONGODB_URI
is missing during the build process, because we need that env actually runtime-only. π