Next.js Discord

Discord Forum

how to make `next dev` use localhost port 3001 if 3000 port is already taken?

Unanswered
Chinese softshell turtle posted this in #help-forum
Open in Discord
Chinese softshell turtleOP
tried searching for it, failed

11 Replies

Chinese softshell turtleOP
next dev -p 3001
i know i can do this
but I want to only do 3001
if 3000 is taken
now it will overwrite existing port
i have another project running on 3000
American Crow
hm it does that automatically for me
Chinese softshell turtleOP
doesn't for me 😿
American Crow
Works with bun for me as well.
Must be some issue with vite or vinxi but i am not familiar with the later
@Chinese softshell turtle doesn't for me 😿
I guess its a vinxi/vite issue as zomh says.

If nothing else works you can write yourself a simple script which will do the same.

#!/bin/bash

port=3000  # Default port
command="bun dev -p $port" 
while lsof -i :$port >/dev/null
do
    echo "Port $port is already in use. Trying a different port..."
    port=$((port + 1)) 
    command="bun dev -p $port" 
done

echo "Running command: $command"
eval $command  # Execute the command