Change the port in Next.js, Vite, Flask and more
Two projects want port 3000, or something else already has it. Every dev server lets you choose another port, but each one does it differently. Here’s the flag, the config setting and the default for each.
By Anmol Sethi · Updated
In short
Pass the port when you start the server: next dev -p 4000, vite --port 4000, ng serve --port 4000, flask run --port 5001, python manage.py runserver 8001 or rails server -p 3001. Through npm, put the flag after --: npm run dev -- --port 4000. To make it stick, set it in the project’s config or package.json script. Portlivo lists which ports are already taken, and by which project.
The cheat sheet
| Tool | Default | One-off | Permanent |
|---|---|---|---|
| Next.js | 3000 | next dev -p 4000 or PORT=4000 | "dev": "next dev -p 4000" in package.json |
| Vite (React, Vue, Svelte, SvelteKit) | 5173 | vite --port 4000 | server: { port: 4000 } in vite.config |
| Create React App | 3000 | PORT=4000 npm start | PORT=4000 in .env |
| Angular | 4200 | ng serve --port 4000 | "port": 4000 under serve options in angular.json |
| Astro | 4321 | astro dev --port 4000 | server: { port: 4000 } in astro.config |
| Nuxt | 3000 | nuxt dev --port 4000 or PORT=4000 | devServer: { port: 4000 } in nuxt.config |
| Express / plain Node | your code | PORT=4000 node server.js | app.listen(process.env.PORT ?? 3000) |
| Flask | 5000 | flask run --port 5001 | app.run(port=5001) |
| FastAPI (uvicorn) | 8000 | uvicorn main:app --port 8001 | the same flag in your run script |
| Django | 8000 | python manage.py runserver 8001 | a script or Makefile target |
| Rails | 3000 | rails server -p 3001 or PORT=3001 | port ENV.fetch("PORT", 3001) in config/puma.rb |
| Laravel | 8000 | php artisan serve --port=8001 | a Composer script |
| Storybook | 6006 | storybook dev -p 6007 | the storybook script in package.json |
Passing the port through npm, pnpm, yarn and bun
With npm, everything after -- goes to the underlying command. pnpm, yarn and bun pass extra flags along without it:
npm run dev -- --port 4000
pnpm dev --port 4000
yarn dev --port 4000
bun run dev --port 4000Use the tool’s own flag after it: --port for Vite, Angular, Astro and Nuxt, and -p or --port for Next.js.
Vite: stop it from picking another port
When 5173 is busy, Vite moves to 5174 without asking, which breaks OAuth callbacks and anything else pinned to a port. strictPort makes it stop with an error instead, so you notice:
// vite.config.ts
export default defineConfig({
server: { port: 5173, strictPort: true },
});Vite doesn’t read the PORT environment variable on its own. To use it, write port: Number(process.env.PORT) || 5173.
Next.js
next dev and next start take -p (or --port) and also read PORT. Put the flag in the script so everyone on the team gets the same port:
"scripts": {
"dev": "next dev -p 4000"
}Next.js has no port setting in next.config.js; the flag or PORT is the way.
Flask and Django
Flask’s default, 5000, is also used by AirPlay Receiver on macOS, so on a Mac it is often taken before you start. Either use another port or turn AirPlay Receiver off.
flask run --port 5001
python manage.py runserver 8001To reach either one from your phone, also listen on the network: flask run --host=0.0.0.0 --port 5001 or runserver 0.0.0.0:8001.
Which port should I use?
- Stay between 1024 and 49151. Lower ports like 80 are reserved for system services, and higher ones are handed out to outgoing connections.
- Avoid ports other software owns on a Mac: 5000 and 7000 (AirPlay), 5432 (Postgres), 3306 (MySQL), 6379 (Redis), 8080 (often a Java or proxy server).
- Give each project its own fixed port, so its bookmarks, OAuth redirects and cookies always line up.
Check a port is free before you commit to it:
lsof -nP -iTCP:4000 -sTCP:LISTEN || echo freeThe one-click way: Portlivo
Before you pick a port, Portlivo shows which ones are taken and by which project, in the menu bar. When a port your project needs is held by something you forgot, stop it with one click instead of moving to a new port.
If a command you start from Portlivo can’t get its port, it tells you who holds it and offers Kill Holder & Retry.
Questions people ask
- Why did my dev server start on 3001 instead of 3000?
- Something was already using 3000, often an old copy of the same project. Next.js and Vite move to the next free port on their own, and Create React App offers to. Stop the old one to get 3000 back, or set the port on purpose.
- How do I pass --port through npm run dev?
- Put
--before the flag:npm run dev -- --port 4000. Without it, npm keeps the flag for itself. pnpm, yarn and bun don’t need the--. - Can two projects use the same port?
- Not at the same time on the same address. Give each project its own port, or stop one before starting the other.
Try Portlivo free for 7 days. Every feature, no account.Then $8.99 once, no subscription.
Download for MacSend to my MacStill have a question?
Write to us — a real developer reads every email and replies personally.