N
Nuxt4mo ago
Wil

code being executed when running npm run build

maybe this is a silly question, but when I run the npm run build command I see that the code in my nitro plugins is being executed. Is this expected? Where can I find more context about this behaviour? :pepethanks:
2 Replies
kapa.ai
kapa.ai4mo ago
Hi @Wil. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- It's a good question! According to the Nuxt documentation, Nitro plugins (i.e., files in your /server/plugins directory or those registered via addServerPlugin) are designed to be executed when the Nitro server starts—this typically means at runtime, not during the build step. The docs state:
When Nitro starts, it initializes and executes the plugins under the /server/plugins directory... Nitro plugins are executed only once when the server starts. In a serverless environment, the server boots on each incoming request, and so do the Nitro plugins. However, they are not awaited.
Nuxt Lifecycle: Server There is no explicit mention in the official documentation that Nitro plugins should be executed during the build process (npm run build). If you are seeing your Nitro plugin code run during the build, this is not the documented or expected behavior. It could be due to a misconfiguration, or possibly a side effect of how your plugin is written or registered. If you want to extend the build process itself, you should use Nuxt hooks or modules, not Nitro plugins. Nitro plugins are for runtime server extension, not build-time logic. For more on the separation between build-time and runtime in Nuxt, see Runtime Context vs. Build Context. If you continue to see this behavior, it may be worth checking for issues in your setup or searching for related issues in the Nuxt or Nitro repositories. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@450801647828860968> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?