fastify-vite icon indicating copy to clipboard operation
fastify-vite copied to clipboard

Unable to use @fastify/middie with @fastify/vite

Open bnason opened this issue 8 months ago • 1 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.3.2

Plugin version

8.1.1

Node.js version

23.10.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Arch

Description

I am unable to use @fastify/middie with @fastify/vite. I receive the following error:

node_modules/fastify/lib/decorate.js:21
    throw new FST_ERR_DEC_ALREADY_PRESENT(name)
    ^
FastifyError [Error]: The decorator 'use' has already been added!
    at decorate (node_modules/fastify/lib/decorate.js:21:11)
    at Object.decorateFastify [as decorate] (node_modules/fastify/lib/decorate.js:77:3)
    at fastifyMiddie (node_modules/@fastify/middie/index.js:28:11)
    at Plugin.exec (node_modules/avvio/lib/plugin.js:125:28)
    at Boot._loadPlugin (node_modules/avvio/boot.js:432:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:90:21) {
  code: 'FST_ERR_DEC_ALREADY_PRESENT',
  statusCode: 500
}

I tried NOT registering middie thinking that the vite plugin had already done it, but that gives this error:

await server.use(() => {
             ^

TypeError: server.use is not a function
    at <anonymous> (src/backend/server.ts:21:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

Link to code that reproduces the bug

https://gist.github.com/bnason/baa21e2c34b827ac074ba147b153566c

Expected Behavior

No response

bnason avatar May 02 '25 13:05 bnason

Interesting — @fastify/vite uses @fastify/middie internally for running Vite development server. And since @fastify/vite registers at the global level, there must be a conflict. I'll investigate.

galvez avatar May 02 '25 14:05 galvez