V5 Migration Guide

This guide is intended to help with migration from Fastify v4 to v5.

Before migrating to v5, please ensure that you have fixed all deprecation warnings from v4. All v4 deprecations have been removed and will no longer work after upgrading.

Long Term Support Cycle

Fastify v5 will only support Node.js v20+. If you are using an older version of Node.js, you will need to upgrade to a newer version to use Fastify v5.

Fastify v4 is still supported until June 30, 2025. If you are unable to upgrade, you should consider buying an end-of-life support plan from HeroDevs.

Why Node.js v20?

Fastify v5 will only support Node.js v20+ because it has significant differences compared to v18, such as better support for node:test. This allows us to provide a better developer experience and streamline maintenance.

Node.js v18 will exit Long Term Support on April 30, 2025, so you should be planning to upgrade to v20 anyway.

Breaking Changes

Full JSON Schema is now required for querystring, params and body and response schemas

Starting with v5, Fastify will require a full JSON schema for the querystring, params and body schema. Note that the jsonShortHand option has been removed as well.

If the default JSON Schema validator is used, you will need to provide a full JSON schema for the querystring, params, body, and response schemas, including the type property.

  1. // v4
  2. fastify.get('/route', {
  3. schema: {
  4. querystring: {
  5. name: { type: 'string' }
  6. }
  7. }
  8. }, (req, reply) => {
  9. reply.send({ hello: req.query.name });
  10. });
  1. // v5
  2. fastify.get('/route', {
  3. schema: {
  4. querystring: {
  5. type: 'object',
  6. properties: {
  7. name: { type: 'string' }
  8. },
  9. required: ['name']
  10. }
  11. }
  12. }, (req, reply) => {
  13. reply.send({ hello: req.query.name });
  14. });

See #5586 for more details

Note that it’s still possible to override the JSON Schema validator to use a different format, such as Zod. This change simplifies that as well.

This change helps with integration of other tools, such as @fastify/swagger.

New logger constructor signature

In Fastify v4, Fastify accepted the options to build a pino logger in the logger option, as well as a custom logger instance. This was the source of significant confusion.

As a result, the logger option will not accept a custom logger anymore in v5. To use a custom logger, you should use the loggerInstance option instead:

  1. // v4
  2. const logger = require('pino')();
  3. const fastify = require('fastify')({
  4. logger
  5. });
  1. // v5
  2. const loggerInstance = require('pino')();
  3. const fastify = require('fastify')({
  4. loggerInstance
  5. });

useSemicolonDelimiter false by default

Starting with v5, Fastify instances will no longer default to supporting the use of semicolon delimiters in the query string as they did in v4. This is due to it being non-standard behavior and not adhering to RFC 3986.

If you still wish to use semicolons as delimiters, you can do so by setting useSemicolonDelimiter: true in the server configuration.

  1. const fastify = require('fastify')({
  2. useSemicolonDelimiter: true
  3. });

The parameters object no longer has a prototype

In v4, the parameters object had a prototype. This is no longer the case in v5. This means that you can no longer access properties inherited from Object on the parameters object, such as toString or hasOwnProperty.

  1. // v4
  2. fastify.get('/route/:name', (req, reply) => {
  3. console.log(req.params.hasOwnProperty('name')); // true
  4. return { hello: req.params.name };
  5. });
  1. // v5
  2. fastify.get('/route/:name', (req, reply) => {
  3. console.log(Object.hasOwn(req.params, 'name')); // true
  4. return { hello: req.params.name };
  5. });

This increases the security of the application by hardening against prototype pollution attacks.

Type Providers now differentiate between validator and serializer schemas

In v4, the type providers had the same types for both validation and serialization. In v5, the type providers have been split into two separate types: ValidatorSchema and SerializerSchema.

@fastify/type-provider-json-schema-to-ts and @fastify/type-provider-typebox have already been updated: upgrade to the latest version to get the new types. If you are using a custom type provider, you will need to modify it like the following:

  1. --- a/index.ts
  2. +++ b/index.ts
  3. @@ -11,7 +11,8 @@ import {
  4. import { FromSchema, FromSchemaDefaultOptions, FromSchemaOptions, JSONSchema } from 'json-schema-to-ts'
  5. export interface JsonSchemaToTsProvider<
  6. Options extends FromSchemaOptions = FromSchemaDefaultOptions
  7. > extends FastifyTypeProvider {
  8. - output: this['input'] extends JSONSchema ? FromSchema<this['input'], Options> : unknown;
  9. + validator: this['schema'] extends JSONSchema ? FromSchema<this['schema'], Options> : unknown;
  10. + serializer: this['schema'] extends JSONSchema ? FromSchema<this['schema'], Options> : unknown;
  11. }

Changes to the .listen() method

The variadic argument signature of the .listen() method has been removed. This means that you can no longer call .listen() with a variable number of arguments.

  1. // v4
  2. fastify.listen(8000)

Will become:

  1. // v5
  2. fastify.listen({ port: 8000 })

This was already deprecated in v4 as FSTDEP011, so you should have already updated your code to use the new signature.

Direct return of trailers has been removed

In v4, you could directly return trailers from a handler. This is no longer possible in v5.

  1. // v4
  2. fastify.get('/route', (req, reply) => {
  3. reply.trailer('ETag', function (reply, payload) {
  4. return 'custom-etag'
  5. })
  6. reply.send('')
  7. });
  1. // v5
  2. fastify.get('/route', (req, reply) => {
  3. reply.trailer('ETag', async function (reply, payload) {
  4. return 'custom-etag'
  5. })
  6. reply.send('')
  7. });

A callback could also be used. This was already deprecated in v4 as FSTDEP013, so you should have already updated your code to use the new signature.

Streamlined access to route definition

All deprecated properties relating to accessing the route definition have been removed and are now accessed via request.routeOptions.

CodeDescriptionHow to solveDiscussion
FSTDEP012You are trying to access the deprecated request.context property.Use request.routeOptions.config or request.routeOptions.schema.#4216 #5084
FSTDEP015You are accessing the deprecated request.routeSchema property.Use request.routeOptions.schema.#4470
FSTDEP016You are accessing the deprecated request.routeConfig property.Use request.routeOptions.config.#4470
FSTDEP017You are accessing the deprecated request.routerPath property.Use request.routeOptions.url.#4470
FSTDEP018You are accessing the deprecated request.routerMethod property.Use request.routeOptions.method.#4470
FSTDEP019You are accessing the deprecated reply.context property.Use reply.routeOptions.config or reply.routeOptions.schema.#5032 #5084

See #5616 for more information.

reply.redirect() has a new signature

The reply.redirect() method has a new signature: reply.redirect(url: string, code?: number).

  1. // v4
  2. reply.redirect(301, '/new-route')

Change it to:

  1. // v5
  2. reply.redirect('/new-route', 301)

This was already deprecated in v4 as FSTDEP021, so you should have already updated your code to use the new signature.

Modifying reply.sent is now forbidden

In v4, you could modify the reply.sent property to prevent the response from being sent. This is no longer possible in v5, use reply.hijack() instead.

  1. // v4
  2. fastify.get('/route', (req, reply) => {
  3. reply.sent = true;
  4. reply.raw.end('hello');
  5. });

Change it to:

  1. // v5
  2. fastify.get('/route', (req, reply) => {
  3. reply.hijack();
  4. reply.raw.end('hello');
  5. });

This was already deprecated in v4 as FSTDEP010, so you should have already updated your code to use the new signature.

Constraints for route versioning signature changes

We changed the signature for route versioning constraints. The version and versioning options have been removed and you should use the constraints option instead.

CodeDescriptionHow to solveDiscussion
FSTDEP008You are using route constraints via the route {version: “…”} option.Use {constraints: {version: “…”}} option.#2682
FSTDEP009You are using a custom route versioning strategy via the server {versioning: “…”} option.Use {constraints: {version: “…”}} option.#2682

HEAD routes requires to register before GET when exposeHeadRoutes: true

We have a more strict requirement for custom HEAD route when exposeHeadRoutes: true.

When you provides a custom HEAD route, you must either explicitly set exposeHeadRoutes to false

  1. // v4
  2. fastify.get('/route', {
  3. }, (req, reply) => {
  4. reply.send({ hello: 'world' });
  5. });
  6. fastify.head('/route', (req, reply) => {
  7. // ...
  8. });
  1. // v5
  2. fastify.get('/route', {
  3. exposeHeadRoutes: false
  4. }, (req, reply) => {
  5. reply.send({ hello: 'world' });
  6. });
  7. fastify.head('/route', (req, reply) => {
  8. // ...
  9. });

or place the HEAD route before GET.

  1. // v5
  2. fastify.head('/route', (req, reply) => {
  3. // ...
  4. });
  5. fastify.get('/route', {
  6. }, (req, reply) => {
  7. reply.send({ hello: 'world' });
  8. });

This was changed in #2700, and the old behavior was deprecated in v4 as FSTDEP007.

Removed request.connection

The request.connection property has been removed in v5. You should use request.socket instead.

  1. // v4
  2. fastify.get('/route', (req, reply) => {
  3. console.log(req.connection.remoteAddress);
  4. return { hello: 'world' };
  5. });
  1. // v5
  2. fastify.get('/route', (req, reply) => {
  3. console.log(req.socket.remoteAddress);
  4. return { hello: 'world' };
  5. });

This was already deprecated in v4 as FSTDEP05, so you should have already updated your code to use the new signature.

reply.getResponseTime() has been removed, use reply.elapsedTime instead

The reply.getResponseTime() method has been removed in v5. You should use reply.elapsedTime instead.

  1. // v4
  2. fastify.get('/route', (req, reply) => {
  3. console.log(reply.getResponseTime());
  4. return { hello: 'world' };
  5. });
  1. // v5
  2. fastify.get('/route', (req, reply) => {
  3. console.log(reply.elapsedTime);
  4. return { hello: 'world' };
  5. });

This was already deprecated in v4 as FSTDEP20, so you should have already updated your code to use the new signature.

fastify.hasRoute() now matches the behavior of find-my-way

The fastify.hasRoute() method now matches the behavior of find-my-way and requires the route definition to be passed as it is defined in the route.

  1. // v4
  2. fastify.get('/example/:file(^\\d+).png', function (request, reply) { })
  3. console.log(fastify.hasRoute({
  4. method: 'GET',
  5. url: '/example/12345.png'
  6. )); // true
  1. // v5
  2. fastify.get('/example/:file(^\\d+).png', function (request, reply) { })
  3. console.log(fastify.hasRoute({
  4. method: 'GET',
  5. url: '/example/:file(^\\d+).png'
  6. )); // true

Removal of some non-standard HTTP methods

We have removed the following HTTP methods from Fastify:

  • PROPFIND
  • PROPPATCH
  • MKCOL
  • COPY
  • MOVE
  • LOCK
  • UNLOCK
  • TRACE
  • SEARCH

It’s now possible to add them back using the acceptHTTPMethod method.

  1. const fastify = Fastify()
  2. // add a new http method on top of the default ones:
  3. fastify.acceptHTTPMethod('REBIND')
  4. // add a new HTTP method that accepts a body:
  5. fastify.acceptHTTPMethod('REBIND', { hasBody: true })
  6. // reads the HTTP methods list:
  7. fastify.supportedMethods // returns a string array

See #5567 for more information.

Removed support from reference types in decorators

Decorating Request/Reply with a reference type (Array, Object) is now prohibited as this reference is shared amongst all requests.

  1. // v4
  2. fastify.decorateRequest('myObject', { hello: 'world' });
  1. // v5
  2. fastify.decorateRequest('myObject');
  3. fastify.addHook('onRequest', async (req, reply) => {
  4. req.myObject = { hello: 'world' };
  5. });

or turn it into a function

  1. // v5
  2. fastify.decorateRequest('myObject', () => { hello: 'world' });

or as a getter

  1. // v5
  2. fastify.decorateRequest('myObject', {
  3. getter () {
  4. return { hello: 'world' }
  5. }
  6. });

See #5462 for more information.

Remove support for DELETE with a Content-Type: application/json header and an empty body

In v4, Fastify allowed DELETE requests with a Content-Type: application/json header and an empty body was accepted. This is no longer allowed in v5.

See #5419 for more information.

Plugins cannot mix callback/promise API anymore

In v4, plugins could mix the callback and promise API, leading to unexpected behavior. This is no longer allowed in v5.

  1. // v4
  2. fastify.register(async function (instance, opts, done) {
  3. done();
  4. });
  1. // v5
  2. fastify.register(async function (instance, opts) {
  3. return;
  4. });

or

  1. // v5
  2. fastify.register(function (instance, opts, done) {
  3. done();
  4. });

Removes getDefaultRoute and setDefaultRoute methods

The getDefaultRoute and setDefaultRoute methods have been removed in v5.

See #4485 and #4480 for more information. This was already deprecated in v4 as FSTDEP014, so you should have already updated your code.

New Features

Diagnostic Channel support

Fastify v5 now supports the Diagnostic Channel API natively and provides a way to trace the lifecycle of a request.

  1. 'use strict'
  2. const diagnostics = require('node:diagnostics_channel')
  3. const sget = require('simple-get').concat
  4. const Fastify = require('fastify')
  5. diagnostics.subscribe('tracing:fastify.request.handler:start', (msg) => {
  6. console.log(msg.route.url) // '/:id'
  7. console.log(msg.route.method) // 'GET'
  8. })
  9. diagnostics.subscribe('tracing:fastify.request.handler:end', (msg) => {
  10. // msg is the same as the one emitted by the 'tracing:fastify.request.handler:start' channel
  11. console.log(msg)
  12. })
  13. diagnostics.subscribe('tracing:fastify.request.handler:error', (msg) => {
  14. // in case of error
  15. })
  16. const fastify = Fastify()
  17. fastify.route({
  18. method: 'GET',
  19. url: '/:id',
  20. handler: function (req, reply) {
  21. return { hello: 'world' }
  22. }
  23. })
  24. fastify.listen({ port: 0 }, function () {
  25. sget({
  26. method: 'GET',
  27. url: fastify.listeningOrigin + '/7'
  28. }, (err, response, body) => {
  29. t.error(err)
  30. t.equal(response.statusCode, 200)
  31. t.same(JSON.parse(body), { hello: 'world' })
  32. })
  33. })

See the documentation and #5252 for additional details.