Auto Imports
Automatic imports for utilities and composables.
nitro.config.ts
import { defineConfig } from "nitro";
export default defineConfig({
serverDir: true,
imports: {},
});
Functions exported from server/utils/ are automatically available without explicit imports when auto-imports are enabled. Define a utility once and use it anywhere in your server code.
Configuration
Enable auto-imports by setting imports in your config:
nitro.config.ts
import { defineConfig } from "nitro";
export default defineConfig({
serverDir: true,
imports: {},
});
Using Auto Imports
- Create a utility file in
server/utils/:
hello.ts
export function makeGreeting(name: string) {
return `Hello, ${name}!`;
}
- The function is available without importing it:
server.ts
import { defineHandler } from "nitro/h3";
import { makeGreeting } from "./server/utils/hello.ts";
export default defineHandler(() => `<h1>${makeGreeting("Nitro")}</h1>`);
With this setup, any function exported from server/utils/ becomes globally available. Nitro scans the directory and generates the necessary imports automatically.