const fs = require('fs'); const path = require('path'); // Path to the original file const filePath = path.join( process.cwd(), 'node_modules/next/dist/server/dev/hot-reloader-webpack.js' ); // Read the original file let content = fs.readFileSync(filePath, 'utf8'); // Check if the file contains the problematic code if (content.includes('wsServer.handleUpgrade(req, req.socket, head, (client)=>{')) { console.log('Applying WebSocket server patch to Next.js hot-reloader-webpack.js'); // Fix: Add WebSocket server initialization content = content.replace( 'onHMR(req, _socket, head) {\n wsServer.handleUpgrade', 'onHMR(req, _socket, head) {\n // Create WebSocket server if not available\n const WebSocketServer = require("ws").Server;\n const wsServer = new WebSocketServer({ noServer: true });\n wsServer.handleUpgrade' ); // Write the patched file back fs.writeFileSync(filePath, content); console.log('Patch applied successfully'); } else { console.log('The file does not contain the expected code or has already been patched'); }