fastify/fastify-http-proxy

Plugin must be a function or a promise. TypeScript project.

wesbragagt opened this issue · 7 comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure it has not already been reported

Fastify version

3.19.1

Plugin version

6.0.0

Node.js version

15

Operating system

Linux

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

Ubuntu 20.04

Description

I followed the documentation steps for the fastify-cli and fastify-http-proxy in order to implement the plugin and I keep getting the same error. Error: plugin must be a function or a promise.

Steps to Reproduce

  • Start a Fastify project using fastify generate project --lang=ts
  • cd project && npm install
  • paste the following code under app.ts
import { join } from 'path'
import AutoLoad, { AutoloadPluginOptions } from 'fastify-autoload'
import { FastifyPluginAsync } from 'fastify'
import fastifyHttpProxy from 'fastify-http-proxy'

// look into implementing this package https://github.com/fastify/fastify-http-proxy

export type AppOptions = {
    // Place your custom options for app below here.
} & Partial<AutoloadPluginOptions>

const app: FastifyPluginAsync<AppOptions> = async (
    fastify,
    opts
): Promise<void> => {
    // Place here your custom code!
    fastify.register(fastifyHttpProxy, {
        upstream: 'https://jsonplaceholder.typicode.com/todos',
        prefix: '/todos'
    })
    // Do not touch the following lines

    // This loads all plugins defined in plugins
    // those should be support plugins that are reused
    // through your application
    void fastify.register(AutoLoad, {
        dir: join(__dirname, 'plugins'),
        options: opts
    })

    // This loads all plugins defined in routes
    // define your routes in one of these
    void fastify.register(AutoLoad, {
        dir: join(__dirname, 'routes'),
        options: opts
    })
}

export default app
export { app }
  • npm run dev

Expected Behavior

You should get an error in the terminal saying "Error: plugin must be a function or a promise"

image

cc @fastify/typescript

It is not Typescript error but an avvio error. I think it is cause by the fastify-autoload. Some of the files inside either folder plugins or routes do not export a correct function.

Still the same issue without Autoload
image

Still the same issue without Autoload
image

Check the content in ./modules/db and ./modules/routes/vehicles.

Pretty standard, I tried commenting those two plug-in registers and still got the same error. But I did try a project without typescript and that seemed to work well. I will post a project sample shortly on this thread with the same error described above.

@wesbragagt Can you check if v6.0.1 solve your problem? Feel free to reopen the issue if it doesn't help.

Man, that did it! I was trying to replicate it in a demo repo so I could share it with you guys. Anyways, appreciate the support!