A Fastify plugin decorates the instance with a database client. A second plugin, registered right after it, reads fastify.db and gets undefined. Nothing is misspelled. This is Fastify working as designed, and once the rule is clear it stops being surprising and starts being useful.
Every register creates a context
The documentation calls it the encapsulation context: it "governs which decorators, registered hooks, and plugins are available to routes." Each call to register creates a new one, and the rules are short:
- A context inherits from its parents. A plugin can use decorators and hooks registered above it.
- Parents cannot see into children. "Parent contexts cannot access entities within their descendant contexts."
- Siblings cannot see each other. The plugins guide shows a second sibling calling a utility decorated in the first, and says the call "will throw an error", because the utility "exists only inside the first register context."
That third rule is the one behind the undefined. Two plugins registered one after the other on the root are siblings. Whatever the first decorates stays inside it.
fastify-plugin breaks the boundary, for one plugin
When a plugin exists to provide something to the rest of the app — a database connection, a config object, an auth helper — wrap it with fastify-plugin. That makes what it registers visible to its parent instead of only to itself. This is the plugins guide's pattern for a shared connection, written here as an async function rather than with the guide's done callback:
const fp = require('fastify-plugin')
async function dbPlugin (fastify, opts) {
const conn = await dbClient.connect(opts.url)
fastify.decorate('db', conn)
}
module.exports = fp(dbPlugin)Two limits to know. First, it only applies to the plugin it wraps: the documentation notes that plugins registered inside it without fastify-plugin "still create new encapsulated contexts." Second, it is the exception. Most plugins — a group of routes, a feature area — should stay encapsulated.
Encapsulation is the feature, not the obstacle
The same rule that hid the database client is what lets you scope behaviour cleanly. Register an authentication hook inside the plugin that holds the admin routes, and it runs for those routes and nothing else. Public routes registered in a sibling never see it. There is no list of excluded paths to maintain, because the hook simply is not in their context.
A practical structure follows: shared services at the root, wrapped with fastify-plugin; each feature area as its own encapsulated plugin, carrying its own hooks and decorators.
Other documented traps
- Decorators are not ready until the app is. Values decorated inside plugins are not accessible before
.listen(),.inject()or.ready(), because plugins only load then. Reading one at module top level gets nothing. - Signal completion. A plugin must return a promise (an
asyncfunction does) or calldone. Do neither and loading stalls. - No arrow functions for request and reply decorators. A method added with
decorateRequestordecorateReplyneedsthis, and arrow functions do not have their own. - Options can come from the parent.
registeraccepts a function for options, called with the parent instance, so a plugin can be configured with something an earlier plugin decorated:parent => ({ connection: parent.db }).
The quick diagnosis
When something registered in one plugin is missing in another, ask where each was registered relative to the other. Parent to child works. Child to parent, or sibling to sibling, needs fastify-plugin — or, more often, the thing belongs one level higher in the first place.