@fastify/autoload

Automatically loads all plugins and routes from a directory tree.

Since Fastify 5 Spec ↗

Syntax

app.register(autoLoad, { dir, options? })

Parameters

NameTypeRequiredDescription
options object No dir to scan, plus matchFilter, ignoreFilter, and dirNameRoutePrefix.

Returns

Promise<void> — Resolves when all discovered plugins are registered.

Examples

import Fastify from 'fastify';
import autoLoad from '@fastify/autoload';
import { join } from 'node:path';

const app = Fastify();

await app.register(autoLoad, {
  dir: join(import.meta.dirname, 'plugins'),
});

await app.register(autoLoad, {
  dir: join(import.meta.dirname, 'routes'),
  options: { prefix: '/api' },
});

Notes

Convention-based loading: a `plugins/` folder for shared infrastructure (wrap with fastify-plugin) and a `routes/` folder where subdirectories become route prefixes. This is the structure the Fastify CLI scaffolds.