@fastify/autoload
Auto-load Fastify plugins from a directory — organize routes and plugins as files and let the framework discover them automatically.
@fastify/autoload
FastifyAuto-load Fastify plugins from a directory — organize routes and plugins as files and let the framework discover them automatically.
Fit
Bundle (gzip)
3.8 kB
9.7 kB raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
As a Fastify app grows, manually registering every route plugin with app.register() in the main file becomes a maintenance burden. File-based routing conventions (like Next.js uses) make the codebase much easier to navigate.
What It Does
@fastify/autoload scans a directory recursively and registers every .js/.ts file that exports a Fastify plugin. Directory structure maps to URL prefixes (e.g. routes/users/index.ts → /users). Plugins can declare their own prefix, and the load order respects dependencies.
Installation
npm install @fastify/autoloadUsage Example
import fastify from 'fastify';
import autoload from '@fastify/autoload';
import { join } from 'path';
const app = fastify();
// Auto-register all plugins in ./plugins
await app.register(autoload, { dir: join(__dirname, 'plugins') });
// Auto-register all routes in ./routes (maps dir structure to URL prefix)
await app.register(autoload, { dir: join(__dirname, 'routes') });
// routes/users/index.ts → GET /users
// routes/users/:id.ts → GET /users/:idRelated packages
Plugin for Fastify that adds support for reading and setting cookies, including signed cookies.
@fastify/corsFastifyCORS plugin for Fastify — configure cross-origin resource sharing with full support for preflight requests and dynamic origins.
@fastify/helmetFastifySecurity headers plugin for Fastify — sets HTTP headers that protect against common web vulnerabilities like XSS, clickjacking, and MIME sniffing.
@fastify/jwtFastifyJWT authentication plugin for Fastify — sign, verify, and decode tokens with a decorator-based API that integrates with Fastify's request lifecycle.
@fastify/multipartFastifyMultipart form data / file upload plugin for Fastify — stream files directly to storage without buffering in memory.