Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastifyError when using fastify-express with Nest.js and Fastify #61

Closed
2 tasks done
amOgury opened this issue Nov 30, 2021 · 3 comments
Closed
2 tasks done

FastifyError when using fastify-express with Nest.js and Fastify #61

amOgury opened this issue Nov 30, 2021 · 3 comments

Comments

@amOgury
Copy link

amOgury commented Nov 30, 2021

Prerequisites

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

Fastify version

3.24.0

Plugin version

0.3.3

Node.js version

14.17.6

Operating system

macOS

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

11.6

Description

When I run npm run start on minimal example with nest.js and fastify-express, I have following:

antonmalmygin@Antons-MBP project-name % npm run start

> project-name@0.0.1 start
> nest start

[Nest] 10969  - 11/30/2021, 12:59:26 PM     LOG [NestFactory] Starting Nest application...
[Nest] 10969  - 11/30/2021, 12:59:26 PM     LOG [InstanceLoader] AppModule dependencies initialized +18ms
(node:10969) UnhandledPromiseRejectionWarning: FastifyError: The decorator 'use' has already been added!
    at decorate (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/fastify/lib/decorate.js:22:11)
    at Object.decorateFastify [as decorate] (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/fastify/lib/decorate.js:65:3)
    at expressPlugin (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/fastify-express/index.js:9:11)
    at Plugin.exec (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/avvio/plugin.js:132:19)
    at Boot.loadPlugin (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/avvio/plugin.js:267:10)
    at release (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/fastq/queue.js:149:16)
    at Object.resume (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/fastq/queue.js:82:7)
    at setup (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/avvio/plugin.js:164:12)
    at Plugin.loadedSoFar (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/avvio/plugin.js:176:7)
    at Boot._loadRegistered (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/avvio/boot.js:227:17)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:10969) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10969) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:10969) UnhandledPromiseRejectionWarning: Error: ERR_AVVIO_PLUGIN_TIMEOUT: plugin did not start in time: /Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/fastify-express/index.js. You may have forgotten to call 'done' function or to resolve a Promise
    at Timeout._onTimeout (/Users/antonmalmygin/Desktop/Dev/another/nest-example/project-name/node_modules/avvio/plugin.js:123:19)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)
(node:10969) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

I saw similar issue being reported here #36, but changing Fastify version doesn't solved the issue for me (and also I use Nest.js).

Steps to Reproduce

I created a minimal example to reproduce here https://github.com/amOgury/nestjs-fastify-express-example

It's identical to Nest.js example with Fastify from here https://docs.nestjs.com/techniques/performance, except that I register fastify-express here https://github.com/amOgury/nestjs-fastify-express-example/blob/main/src/main.ts#L13

Expected Behavior

Minimal example should start normally

@mcollina
Copy link
Member

I think Nest.js already register a plugin for this. I think you might want to open an issue there. This is not a problem for this module.

@biaomingzhong
Copy link

I also encountered the same problem, how did you deal with the problem afterwards. @amOgury

@pond918
Copy link

pond918 commented Feb 5, 2024

I decorate response as below, to compatible with express:

  // in main.ts
  // express compatibility
  const fastifyInstance = app.getHttpAdapter().getInstance();
  fastifyInstance
    .decorateReply('set', function (header: any) {
      const [key, value] = Object.entries(header)[0];
      this.header(key, value);
    })
    .decorateReply('status', function (code: number) {
      return this.code(code);
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants