@efebia/fastify-auto-import

中文文档
1.2.1发布于 2026/11/12MITTypeScriptESM + CJS构建来源已验证近 30 天下载 2991

Fastify plugin that automatically discovers and registers route plugins from a directory.

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. API
  4. autoImport (default export)
  5. Recursive mode
  6. File-based routing
  7. License

@efebia/fastify-auto-import

Fastify plugin that automatically discovers and registers route plugins from a directory.

Install

npm install @efebia/fastify-auto-import

Usage

import Fastify from 'fastify';
import autoImport from '@efebia/fastify-auto-import';

const fastify = Fastify();

await fastify.register(autoImport, {
  // Required: the starting directory (use import.meta.url in ESM)
  startingDirectory: import.meta.url,
  // Directory to scan, relative to startingDirectory (default: 'src/plugins')
  directory: 'src/plugins',
  // File name to import from each subdirectory (default: 'routes')
  routeFile: 'routes',
  // Subdirectories to skip (default: [])
  excludedDirectories: ['_shared'],
});

Given the following structure:

src/plugins/
  users/
    routes.ts
  orders/
    routes.ts
  _shared/
    utils.ts   ← skipped

The plugin will register users/routes.ts and orders/routes.ts as Fastify plugins.

Each route file must export a default Fastify plugin:

// src/plugins/users/routes.ts
import type { FastifyPluginAsync } from 'fastify';

const plugin: FastifyPluginAsync = async (fastify) => {
  fastify.get('/users', async () => ({ users: [] }));
};

export default plugin;

API

autoImport (default export)

A fastify-plugin compatible plugin. Register it with fastify.register().

Options (Globber):

OptionTypeDefaultDescription
startingDirectorystring—Required. Base directory. Pass import.meta.url (ESM) or __dirname (CJS).
directorystring'src/plugins'Directory to scan for route subdirectories, relative to startingDirectory.
routeFilestring'routes'File name (without extension) to import from each subdirectory.
excludedDirectoriesstring[][]Subdirectory names to skip.
recursivebooleanfalseScan nested subdirectories for route files, not just direct children.
fileBasedRoutingbooleanfalseDerive each folder's URL prefix from its path. See below.
logpino.BaseLoggerFastify loggerLogger instance for import errors.

Recursive mode

By default the plugin only looks at direct subdirectories of directory. Set recursive: true to walk the entire tree and register every subdirectory that contains a route file, at any depth.

await fastify.register(autoImport, {
  startingDirectory: import.meta.url,
  directory: 'src/plugins',
  recursive: true,
});

Given this structure:

src/plugins/
  users/
    routes.ts          ← registered
    profile/
      routes.ts        ← registered (nested)
  orders/
    routes.ts          ← registered
    items/
      routes.ts        ← registered (nested)
  _shared/
    utils.ts           ← skipped (no routes file)

Without recursive, only users/routes.ts and orders/routes.ts would be registered. With recursive: true, users/profile/routes.ts and orders/items/routes.ts are picked up as well.

excludedDirectories still applies at every depth — a matching directory name is skipped along with all its descendants.

File-based routing

Set fileBasedRouting: true to make folders the URL. It walks the whole tree (no need for recursive).

src/routes/
  api/
    users/
      routes.ts            ← /api/users
      get.ts               ← plain handler, imported by routes.ts (not auto-loaded)
      [userId]/
        routes.ts          ← /api/users/:userId
    (private)/
      tasks/routes.ts      ← /api/tasks  ((private) adds no segment)
  • The full folder path is the URL. Each routes.ts is registered flat, in its own encapsulated scope, with that path as prefix, so it only declares paths relative to its folder (app.get('/', ...), app.get('/export', ...)).
  • Scopes are independent: hooks in a parent folder's routes.ts never apply to child folders (and vice versa).
  • [id] becomes :id. Folders named _x or (group) add no segment.
  • Only the routes file is loaded. Anything else in the folder (handlers, schemas) is yours to import from it.
  • Middleware is not built in. Add hooks or preHandlers in each routes.ts that needs them.
  • excludedDirectories and routeFile behave as usual.

License

MIT

版本与发布记录

@efebia/fastify-auto-import 共发布 9 个版本,首次发布于 2023/01/13;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.1latest2026/11/121 天前
1.2.02026/09/211 个月前
1.1.12026/08/113 个月前
1.1.02026/05/086 个月前
1.0.32026/03/197 个月前
1.0.12025/04/131.6 年前

关于 @efebia/fastify-auto-import 的常见问题

@efebia/fastify-auto-import 是什么?

Fastify plugin that automatically discovers and registers route plugins from a directory.

如何安装 @efebia/fastify-auto-import?

在项目目录运行 npm install @efebia/fastify-auto-import,或使用 pnpm add / yarn add / bun add。

@efebia/fastify-auto-import 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@efebia/fastify-auto-import 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@efebia/fastify-auto-import 可以商用吗?

@efebia/fastify-auto-import 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@efebia/fastify-auto-import 的下载量有多少?

根据 npm 官方下载统计,@efebia/fastify-auto-import 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,991 次,日均约 100 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@efebia/fastify-auto-import 还在维护吗?

最新版本 1.2.1 发布于 2026/11/12(1 天前);至今共发布 9 个版本,其中过去 12 个月发布了 5 个;首次发布于 2023/01/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。