vite-plugin-node icon indicating copy to clipboard operation
vite-plugin-node copied to clipboard

Running vite: Cannot modify fastify route without crashing.

Open skogs-pa opened this issue 3 years ago • 4 comments

Running "vite" as my pnpm dev command. Happens when I edit a route handler.

6:50:39 AM [vite] Error when evaluating SSR module ./src/index.ts:
Error: Cannot add route when fastify instance is already started!

Seems like fastify has issues with SSR. My old command "nodemon" works fine since it reloads the whole server from scratch.

skogs-pa avatar Mar 12 '22 14:03 skogs-pa

can you provide a repro?

I could create new route without any problem.

axe-me avatar May 11 '22 09:05 axe-me

+1

C:\pkg\node_modules\.pnpm\[email protected]\node_modules\fastify\lib\route.js:367
            throw new FST_ERR_DUPLICATED_ROUTE(opts.method, opts.url)
                  ^
FastifyError [Error]: Method 'POST' already declared for route '/auth/login'
    at Object.addNewRoute (C:\pkg\node_modules\.pnpm\[email protected]\node_modules\fastify\lib\route.js:367:19)
    at Object.route (C:\pkg\node_modules\.pnpm\[email protected]\node_modules\fastify\lib\route.js:261:19)
    at Object.prepareRoute (C:\pkg\node_modules\.pnpm\[email protected]\node_modules\fastify\lib\route.js:167:18)
    at Object._post [as post] (C:\pkg\node_modules\.pnpm\[email protected]\node_modules\fastify\fastify.js:270:34)
    at FastifyAdapter.injectRouteOptions (C:\pkg\node_modules\.pnpm\@[email protected]_@[email protected]_@[email protected]\node_modules\@nestjs\platform-fastify\adapters\fastify-adapter.js:392:46)
    at FastifyAdapter.post (C:\pkg\node_modules\.pnpm\@[email protected]_@[email protected]_@[email protected]\node_modules\@nestjs\platform-fastify\adapters\fastify-adapter.js:129:21)
    at C:\pkg\node_modules\.pnpm\@[email protected]_@[email protected]_@[email protected][email protected][email protected]\node_modules\@nestjs\core\router\router-explorer.js:105:17
    at Array.forEach (<anonymous>)
    at C:\pkg\node_modules\.pnpm\@[email protected]_@[email protected]_@[email protected][email protected][email protected]\node_modules\@nestjs\core\router\router-explorer.js:90:29
    at Array.forEach (<anonymous>) {
  code: 'FST_ERR_DUPLICATED_ROUTE',
  statusCode: 500
}
// package.json
"@nestjs/cli": "^10.3.2",
"@nestjs/common": "^10.3.3",
"@nestjs/core": "^10.3.3",
"@nestjs/platform-fastify": "^10.3.3",
"@swc/cli": "^0.3.10",
"@swc/core": "^1.4.2",
"@swc/helpers": "^0.5.6",
"typescript": "^5.3.3",
"vite": "^5.1.4",
"vite-plugin-node": "^3.1.0",
"fastify": "^4.26.1"
// vite.config.ts
import { defineConfig } from "vite";
import { VitePluginNode as vite_plugin_node } from "vite-plugin-node";

export default defineConfig({
    plugins: [
        ...vite_plugin_node({
            adapter: "nest",
            appPath: "./src/index.ts",
            exportName: "App",
            tsCompiler: "swc",
            initAppOnBoot: true,
        }),
    ],
});
// ./src/index.ts
import { NestFactory } from "@nestjs/core";
import {
  FastifyAdapter,
  type NestFastifyApplication,
} from "@nestjs/platform-fastify";
import { AppModule } from "./app.module";
import { RawServerDefault } from "fastify";

async function create() {
  const adapter = new FastifyAdapter({});
  const app: NestFastifyApplication<RawServerDefault> =
    await NestFactory.create<NestFastifyApplication>(AppModule, adapter);
  return app;
}

export const App = create();

AlexRMU avatar Feb 29 '24 16:02 AlexRMU

It seems that this happens when several requests arrive at one endpoint, for some reason they are processed simultaneously and at the same time they try to declare these methods.

AlexRMU avatar Mar 01 '24 12:03 AlexRMU