Skip to main content
Back to contributions
Pull Request
Merged
59.4K

Add h3 types to auto-imports

nuxt/nuxt

Fixed missing auto-imports for h3 types like H3Event, H3Error, and EventHandler in Nuxt server routes

The Problem

When working with Nuxt server routes, developers frequently need to use h3 types like H3Event, H3Error, and EventHandler. However, IDE auto-import suggestions weren’t working for these types, creating friction in the developer experience.

The root cause was that Nitro’s auto-import configuration intentionally filters out uppercase exports using a regex pattern (!/^[A-Z]/.test(n)). While h3 is a transitive dependency of Nuxt/Nitro, TypeScript could resolve imports like import type { H3Event } from "h3", but IDEs wouldn’t offer auto-import suggestions unless developers manually added h3 as a direct project dependency.

This was particularly painful for common use cases:

// Developers had to manually import these types
import type { H3Event, EventHandler } from 'h3'

export default defineEventHandler((event: H3Event) => {
  // Handler logic
})

Adding h3 as a devDependency was a workaround, but it proved insufficient in pnpm workspaces and added unnecessary friction—requiring a direct dependency solely for editor ergonomics.

The Solution

I added explicit h3 type imports to Nitro’s imports presets configuration, bypassing the uppercase export filter. The fix distinguishes between runtime values and type-only imports:

Runtime imports (these are classes that exist at runtime):

  • H3Event
  • H3Error

Type-only imports (pure TypeScript types):

  • EventHandler
  • EventHandlerRequest
  • EventHandlerResponse
  • EventHandlerObject
  • H3EventContext

The implementation adds a presets array to the Nitro imports configuration:

imports: {
  presets: [
    {
      from: 'h3',
      imports: ['H3Event', 'H3Error'],
    },
    {
      from: 'h3',
      imports: [
        'EventHandler',
        'EventHandlerRequest',
        'EventHandlerResponse',
        'EventHandlerObject',
        'H3EventContext',
      ],
      type: true,
    },
  ] as const,
  // ... existing autoImport and dirs config
}

During code review, CodeRabbit suggested applying as const to the entire presets array rather than individual imports for improved type inference—a suggestion I implemented in subsequent commits to follow codebase conventions.

Files Changed

FilePurpose
packages/nitro-server/src/index.tsAdded h3 type presets to Nitro’s imports configuration
docs/2.directory-structure/1.server.mdDocumentation updates (removed per review feedback)

Timeline

DateAction
2025-12-15Issue #33889 reported by jmroon
2026-01-05Analyzed Nitro’s import filtering logic and identified the uppercase export filter
2026-01-05Implemented fix and verified with playground and tests
2026-01-06PR #34035 opened
2026-01-06Applied as const to entire presets array per codebase convention
2026-01-06Awaiting review from @danielroe (code owner)
2026-01-14PR merged by @danielroe, included in v4.3.0