N
Nuxt5mo ago
Norbiros

Zod auto imports

Hi! I am using zod4 and nuxt. I want to do something like this:
import * as z from 'zod'

export default defineNuxtPlugin(() => {
z.config(z.locales.pl())

addImports({ name: '*', as: 'z', from: 'zod' })
})
import * as z from 'zod'

export default defineNuxtPlugin(() => {
z.config(z.locales.pl())

addImports({ name: '*', as: 'z', from: 'zod' })
})
But in the main app, without creating nuxt module. Is it even possible to use addImports here (i get errors about vue parts)?
4 Replies
kapa.ai
kapa.ai5mo ago
Hi @Norbiros. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry, but based on the knowledge sources provided, it is not possible to use addImports (or similar Nuxt Kit utilities) directly inside a Nuxt plugin or in your main app code. The addImports utility is specifically designed to be used within a Nuxt module's setup function, not inside runtime plugin code or application files. Attempting to use it elsewhere will result in errors, especially related to Vue/Nuxt context or build-time limitations. If you want to auto-import Zod as z throughout your app, you must configure this at the build/module level, not at runtime. For example, you can add a preset in your nuxt.config.ts:
export default defineNuxtConfig({
imports: {
presets: [
{
from: 'zod',
imports: [{ name: '*', as: 'z' }]
}
]
}
})
export default defineNuxtConfig({
imports: {
presets: [
{
from: 'zod',
imports: [{ name: '*', as: 'z' }]
}
]
}
})
This will make z available as an auto-import everywhere in your app, without needing to create a custom module or use addImports in a plugin.
Auto-import from Third-Party Packages Summary:
- addImports is for module authors, not for use in plugins or app code. - Use the imports.presets option in nuxt.config.ts for your use case. - Trying to use addImports in a plugin will cause errors, as you experienced. Let me know if you need an example for your nuxt.config.ts! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@770620808644919307> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Norbiros
NorbirosOP5mo ago
ERROR 4:48:15 PM ERROR(vue-tsc) Cannot find namespace 'z'. FILE /home/norbi/Development/SideProjects/Hack4KrakSite/frontend/app/pages/request_password_reset.vue:22:31 20 | // 21 | // )
22 | async function onSubmit(data: z.output<typeof schema>) {
@kapa.ai
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you again. One moment! :books: Sorry @Norbiros, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?

Did you find this page helpful?