Fixing “Port is already in use” in Vite
When a Vite dev server runs with --strictPort or server.strictPort: true and fails with "Port 5173 is already in use", the port is already taken by another program, for example a Vite instance from another project that is still running. Find that program and stop it, or start the server on a different port, and the error goes away.
This is about the case where you are developing several projects at once and npm run dev exits without starting the server. There is also a second case: during startup you may see "WebSocket server error: Port 24678 is already in use". In that case the dev server starts, but saving a file does not update the page (HMR does not work). I reproduced both messages in my test environment and confirmed that fixing the port conflict made them go away.
The test environment was Ubuntu 24.04, Node.js 22.23.5, and Vite 8.3.8. I created the projects with the react-ts template from npm create vite@latest and prepared two copies of it (app and other-app). The browser was headless Chromium from Playwright 1.63.0. I did not test on Windows or macOS, inside a Docker container, or with a configuration that listens on IPv6.
"Port 5173 is already in use" and the dev server exits
With other-app running npx vite, I ran npx vite --strictPort in app. It exited with the following message (exit code 1; the paths are example values).
error when starting dev server:
Error: Port 5173 is already in use
at httpServerStart (file:///path/to/app/node_modules/vite/dist/node/chunks/node.js:14673:10)
at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
at async startServer (file:///path/to/app/node_modules/vite/dist/node/chunks/node.js:25081:30)
at async Object.listen (file:///path/to/app/node_modules/vite/dist/node/chunks/node.js:24887:4)
at async CAC.<anonymous> (file:///path/to/app/node_modules/vite/dist/node/cli.js:734:3)
The same happens when strictPort: true is written in vite.config.ts. With the following configuration, running npm run dev printed the same message after the > vite line and then exited.
// vite.config.ts
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
server: {
port: 5173,
strictPort: true,
},
})
Without strictPort, this error does not stop the server. Vite moves to the next free port and starts there. In the same situation, npx vite started on 5174 as shown below.
Port 5173 is in use, trying another one...
VITE v8.3.8 ready in 174 ms
➜ Local: http://localhost:5174/
➜ Network: use --host to expose
This behavior is described in the official documentation for server.port and server.strictPort. Because the URL changes to 5174 when Vite moves to the next port, add strictPort when you want to always open the same URL. If the URL changing is fine, removing strictPort alone is enough to get the server running.
Find the dev server that is using the port
On Linux, fuser shows the process ID (PID) of the program using a port.
$ fuser 5173/tcp
5173/tcp: 3189
Pass that number to ps to see which program it is.
$ ps -o pid,args -p 3189
PID COMMAND
3189 node /path/to/other-app/node_modules/.bin/vite
Because the command contains node_modules/.bin/vite, it is a Vite dev server. The path also shows which project it belongs to. You can confirm the folder the process was started from with /proc/<PID>/cwd.
$ readlink /proc/3189/cwd
/path/to/other-app
(The paths in both commands are example values.)
Stopping it and starting again clears the error
Stop the dev server you found by specifying its PID.
$ kill 3189
$ fuser 5173/tcp
If the second fuser, run about a second after kill, prints nothing and exits, port 5173 is free (exit code 1). Then I ran npx vite --strictPort in app again. There was no error, and it started on 5173.
VITE v8.3.8 ready in 166 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
Opening http://localhost:5173/ with curl returned 200, and the <title> was the one from app.
If you do not want to stop other-app, start app on a different port instead. With npx vite --port 5180 --strictPort, app started on 5180, and 5173 kept returning the page from other-app. The --port and --strictPort options are described in the official CLI documentation. If you always run the project on a different port, change server.port in vite.config.ts.
"WebSocket server error" when the server starts but HMR does not work
The other "already in use" error appears when the port used by the WebSocket for HMR (the mechanism that updates the page when you save) collides with another one. Normally Vite accepts the WebSocket on the same port as the dev server. In my test environment, without any changes to the configuration, it connected to ws://localhost:5174/.
However, if you set the WebSocket port with server.ws.port and write the same number in two projects, the dev server moves to 5174 but the WebSocket port does not move, so they collide. I wrote the following in the vite.config.ts of both projects (24678 is just an example number).
// vite.config.ts (both app and other-app)
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
server: {
ws: {
port: 24678,
},
},
})
With other-app running, I ran npx vite in app, and the terminal showed the following.
Port 5173 is in use, trying another one...
WebSocket server error: Port 24678 is already in use
VITE v8.3.8 ready in 158 ms
➜ Local: http://localhost:5174/
➜ Network: use --host to expose
The second line is the WebSocket port error. The dev server still keeps running, and opening http://localhost:5174/ shows the page of app.
While the page was open in the browser, I changed the heading in src/App.tsx of app. The terminal printed [vite] (client) hmr update /src/App.tsx, but the heading on the page stayed the same as before the change. The browser console showed the following error. I omitted three things: the value of token in the URL, the two "Failed to send error to Vite server" messages that followed, and the console.error: prefix that my test script added at the beginning of the line.
WebSocket connection to 'ws://localhost:24678/?token=(省略)' failed: Error during WebSocket handshake: Unexpected response code: 400
[vite] failed to connect to websocket (Error: WebSocket closed without opened.).
Running fuser showed that the process using 24678 was the same as the other-app process running on 5173.
$ fuser 5173/tcp 5174/tcp 24678/tcp
5173/tcp: 3491
5174/tcp: 3528
24678/tcp: 3491
The page of app was trying to connect to the WebSocket of other-app, and that connection was rejected.
Use a different WebSocket port
Change server.ws.port in app to an unused number.
server: {
ws: {
port: 24679,
},
},
After restarting, the WebSocket server error line no longer appeared. The browser connected to ws://localhost:24679/, and the console showed [vite] connected.. When I changed the heading, [vite] hot updated: /src/App.tsx appeared, and the heading on the page changed without reloading.
If you do not need to set a WebSocket port, you can also remove the server.ws.port setting altogether. After removing it and starting the server, the WebSocket connected to ws://localhost:5174/, the same port as the dev server, and HMR worked.
The older server.hmr.port option behaves the same way
Older configurations may set the WebSocket port in server.hmr.port. In the official documentation for server.hmr for Vite 8.3.8, port and some other server.hmr options are marked as deprecated, and the docs recommend using server.ws instead. Values written in server.hmr are automatically applied to server.ws, so the older form still works.
In my test environment, setting server.hmr.port to 24678 in both projects produced the same WebSocket server error: Port 24678 is already in use message, and HMR did not work. After I changed server.hmr.port in app to 24679, the error stopped and HMR worked.
"Port 5173 is already in use" is the message for a collision on the dev server port. "WebSocket server error: Port 24678 is already in use" is the message for a collision on the HMR port. The first one exits with code 1 and the server does not start. The second one lets the server start, but HMR does not work, and the browser console shows a 400 error. In both cases, stopping the program using the port or giving the projects different port numbers makes the error go away.





