vite-plugin-node
vite-plugin-node copied to clipboard
Running vite: Cannot modify fastify route without crashing.
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.
can you provide a repro?
I could create new route without any problem.
+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();
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.