8 min read
Building Type-Safe APIs with Nuxt 3 and Prisma
Learn how to build end-to-end type-safe APIs using Nuxt 3 server routes and Prisma ORM for a seamless developer experience.
1.9K
Build custom Nuxt modules to encapsulate functionality, share code between projects, and create powerful development tools.
Mbeah Essilfie
May 20, 2026 at 06:04 AM
Modules are the extension system for Nuxt. They run at build time and can:
// modules/analytics/index.ts
import { defineNuxtModule, addPlugin, createResolver } from '@nuxt/kit'
export default defineNuxtModule({
meta: {
name: 'my-analytics',
configKey: 'analytics',
},
defaults: {
trackingId: '',
debug: false,
},
setup(options, nuxt) {
if (!options.trackingId) {
console.warn('[analytics] No tracking ID provided')
return
}
const resolver = createResolver(import.meta.url)
// Add a plugin that runs on client
addPlugin({
src: resolver.resolve('./runtime/plugin.client'),
mode: 'client',
})
// Make config available at runtime
nuxt.options.runtimeConfig.public.analytics = {
trackingId: options.trackingId,
debug: options.debug,
}
},
})
The runtime plugin:
// modules/analytics/runtime/plugin.client.ts
export default defineNuxtPlugin(() => {
const config = useRuntimeConfig().public.analytics
// Initialize analytics SDK
const analytics = initAnalytics(config.trackingId)
// Track page views on route change
const router = useRouter()
router.afterEach((to) => {
analytics.pageView(to.fullPath)
})
return {
provide: { analytics },
}
})
// modules/analytics/index.ts (in setup)
import { addImports } from '@nuxt/kit'
addImports({
name: 'useAnalytics',
from: resolver.resolve('./runtime/composables/useAnalytics'),
})
// modules/analytics/runtime/composables/useAnalytics.ts
export function useAnalytics() {
const { $analytics } = useNuxtApp()
function trackEvent(name: string, properties?: Record<string, unknown>) {
$analytics.track(name, properties)
}
return { trackEvent }
}
useAnalytics() is auto-imported in every component — no explicit imports needed.// In module setup
import { addServerHandler } from '@nuxt/kit'
addServerHandler({
route: '/api/_analytics/collect',
handler: resolver.resolve('./runtime/server/api/collect.post'),
})
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['./modules/analytics'],
analytics: {
trackingId: 'UA-XXXXX',
debug: process.env.NODE_ENV === 'development',
},
})
Modules are Nuxt's superpower. They turn complex setup into single-line configuration.
Fullstack Software Developer
Learn how to build end-to-end type-safe APIs using Nuxt 3 server routes and Prisma ORM for a seamless developer experience.
Go beyond basic utility classes and learn how to build cohesive, maintainable design systems with Tailwind CSS.
Understand the power of Vue 3's Composition API through practical, real-world composable patterns that you can use today.
