Run Sveltekit Application on user defined port number

Running a SvelteKit App on a Specific Port

development-server
sveltekit
custom port
port configuration
svelte app development

When developing web applications, the ability to run your app on a specific port can be crucial for avoiding conflicts with other processes and for setting up environments that mirror your production setup.

Run Sveltekit App on a specific port

SvelteKit, a popular framework for building high-performance web applications, allows developers to easily set a custom port. To run your SvelteKit application on a specific port, use the following command:

npm run dev -- --host --port 5175

Check if a Port is being used

To see if a specific port, such as 5173, is in use, you can use one of the following commands:

netstat -an | findstr /i " :5173"

without PID

or

netstat -ano | findstr :5173

with PID

Here’s what the output might look like if the port is in use:

C:\Users\sabbirz>netstat -ano | findstr :5173
  TCP    0.0.0.0:5173           0.0.0.0:0              LISTENING       8872
  TCP    0.0.0.0:5174           0.0.0.0:0              LISTENING       5100
  TCP    192.168.100.161:5173   192.168.100.161:60818  ESTABLISHED     8872
  TCP    192.168.100.161:60818  192.168.100.161:5173   ESTABLISHED     26260
  TCP    [::]:5173              [::]:0                 LISTENING       8872
  TCP    [::]:5174              [::]:0                 LISTENING       5100
  TCP    [::1]:5173             [::1]:60819            ESTABLISHED     8872
  TCP    [::1]:5173             [::1]:60824            ESTABLISHED     8872
  TCP    [::1]:60819            [::1]:5173             ESTABLISHED     26260
  TCP    [::1]:60824            [::1]:5173             ESTABLISHED     26260

The last value 8872 is the PID of the process using the port.

How to Close a Port Using PID

If you find that a needed port is in use and you wish to close it, you can terminate the process using its PID.

Step-by-Step Guide to Closing a Port

  1. Identify the PID of the process using the port from the netstat output.
  2. Use the following command to kill the process:
taskkill /PID [PID Value] /F

Safety Tips

Always ensure that you are terminating the correct process. Accidentally closing vital system processes can cause system instability or data loss.