Server plugins not getting invoked

Hi all, I created a new Nuxt app using npx nuxi init. After it was created, I changed directory in to the project, ran npm run dev. Worked ok, browser shows default page. I added file plugins/01.hello.server.ts with content export default defineNuxtPlugin(async () => { console.log('hello nuxt server') }). Ran the app via npm run dev & expected to see hello nuxt server in the terminal, but it didn't show. node --version is v22.15.1 & npm --version is 10.9.2. What gives?
11 Replies
kapa.ai
kapa.ai4mo ago
Hi @matthewadams12. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- It looks like you placed your plugin in the plugins/ directory as 01.hello.server.ts and expected it to run on the server and log to the terminal. However, Nuxt plugins in the plugins/ directory are Nuxt app plugins—they run in the context of the Vue app, not as Nitro server plugins. This means that even with the .server suffix, these plugins are executed during the Nuxt app lifecycle, not at the Nitro server level, and their console.log output may not appear in the terminal as you expect. If you want to run code at the Nitro server level (and see logs in the terminal when the server starts), you should place your plugin in the server/plugins/ directory and use defineNitroPlugin. For example:
// server/plugins/hello.ts
export default defineNitroPlugin(() => {
console.log('hello from nitro server')
})
// server/plugins/hello.ts
export default defineNitroPlugin(() => {
console.log('hello from nitro server')
})
This will ensure your code runs when the Nitro server starts, and you should see the log in your terminal output. The plugins/ directory is for Nuxt app (Vue) plugins, while server/plugins/ is for Nitro server plugins, which are executed once when the server starts up Server directory documentation Nuxt lifecycle: server. In summary:
- Use plugins/ for Nuxt app (Vue) plugins (run in app context, not server context). - Use server/plugins/ for Nitro server plugins (run at server startup, logs appear in terminal). If you want your log to appear in the terminal at server startup, move your plugin to server/plugins/ and use defineNitroPlugin. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@155412273261707264> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
matthewadams12
matthewadams12OP4mo ago
@kapa.ai According to https://nuxt.com/docs/guide/directory-structure/plugins, "You can use .server or .client suffix in the file name to load a plugin only on the server or client side."
kapa.ai
kapa.ai4mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Yes, according to the official Nuxt documentation, you can use the .server or .client suffix in the file name to load a plugin only on the server or client side. For example, a plugin named myPlugin.server.ts will only be loaded on the server, and myPlugin.client.ts will only be loaded on the client. This allows you to control the environment in which your plugin runs without additional configuration plugins directory documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@155412273261707264> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
matthewadams12
matthewadams12OP4mo ago
@kapa.ai Why didn't I see my message hello nuxt server in the terminal, then?
kapa.ai
kapa.ai4mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You did not see your message hello nuxt server in the terminal because plugins in the plugins/ directory—even with the .server suffix—are Nuxt app plugins, not Nitro server plugins. These plugins are executed during the creation of the Vue application on the server, not at the Nitro server level. As a result, their console.log output may not appear in the terminal as you expect during server startup—they are not the same as Nitro server plugins, which are placed in the server/plugins/ directory and are executed when the Nitro server starts plugins directory documentation server directory documentation. In summary:
- .server plugins in plugins/ run in the Nuxt app context, not at the server process level. - To see terminal output at server startup, use a Nitro plugin in server/plugins/ instead. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@155412273261707264> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
matthewadams12
matthewadams12OP4mo ago
@kapa.ai My real project uses Prisma & Supabase. My goal is to seed an admin user at server startup time if one doesn't exist. Should that be done in plugins with defineNuxtPlugin or in server/plugins using defineNitroPlugin?
Cue
Cue4mo ago
The plugins directory is for Vue plugins. The client and server suffixes simply determine which runtime the plugins are applied to Vue. Since you’re looking for server startup execution, you’ll want to use server/plugins.
matthewadams12
matthewadams12OP4mo ago
🙏🏼 ✅

Did you find this page helpful?