-
Hi! Sometimes vite dev server hangs 😑 OK, not a big deal, but the port is already taken and starting new server assigns a new port number. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
OK, just figured it out. In terminal:
then using PID number and
|
Beta Was this translation helpful? Give feedback.
-
I stumbled upon this because I was having trouble taking down a Vite dev server in an Sbt task. The Vite Dev server is a bit finicky, and may stay alive even after killing the parent process. That is, if you start vite with something like this (or an equivalent program in a different programming language): import cp from 'node:child_process';
console.log("Spawning Vite Dev server...");
const p = cp.exec('npx vite');
p.stdout.pipe(process.stdout);
p.stderr.pipe(process.stderr);
console.log("Shutting down in 5 seconds");
await new Promise((resolve) => setTimeout(resolve, 5000));
console.log("Shutting down Vite Dev server...");
p.kill(); Calling The only way that allowed me to have full control of the dev server's lifetime was to spawn a custom script which uses the Vite API directly and actively detects whether it should stop. For instance, if I want this server to live for as long as there is another server listening on local port 9000: import { createServer } from 'vite';
import net from 'node:net';
import url from 'node:url';
console.log("Spawning Vite Dev server...");
const server = await createServer();
await server.listen(5173);
server.printUrls();
const checkPlayServer = async () => {
// check if the server is still alive
let socket = net.connect({
port: 9000,
host: 'localhost',
});
socket.on('error', async (err) => {
console.log("Closing Vite Dev server");
socket.destroy();
await server.close();
})
socket.on('connect', () => {
socket.end();
// connection still OK, resuming pinging
setTimeout(checkPlayServer, 2_500);
})
};
setTimeout(checkPlayServer, 2_500); |
Beta Was this translation helpful? Give feedback.
-
just use this command: |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
OK, just figured it out. In terminal:
then using PID number and
-9
for force kill