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

Running vite: Cannot modify fastify route without crashing. #33

Open
skogs-pa opened this issue Mar 12, 2022 · 3 comments
Open

Running vite: Cannot modify fastify route without crashing. #33

skogs-pa opened this issue Mar 12, 2022 · 3 comments

Comments

@skogs-pa
Copy link

skogs-pa commented Mar 12, 2022

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.

@axe-me
Copy link
Owner

axe-me commented May 11, 2022

can you provide a repro?

I could create new route without any problem.

@AlexRMU
Copy link

AlexRMU commented Feb 29, 2024

+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
Copy link

AlexRMU commented Mar 1, 2024

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.

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

3 participants