`npm run dev` is a long-running program

I got asked this question:

Each time I run npm run dev I get a different localhost port. First it was 3000. Then I ran it again and got port 3001. Then I ran it again and got port 3002. How do we force it to use port 3000?

When working on …


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

I got asked this question:

Each time I run npm run dev I get a different localhost port. First it was 3000. Then I ran it again and got port 3001. Then I ran it again and got port 3002. How do we force it to use port 3000?

When working on a website locally, you use the command npm run dev to start the development server.

This is a common practice in Web Development, and all tools seem to converge to this program, for example Astro and Next.js and many others.

Each time you run this command, it’s a long-running process. It does not end automatically

For example you run the Astro dev server, and it starts listening on port 3000:

Then you open another terminal and you run npm run dev again, this time you got the server running on port 3001:

So now you have the same app running on

http://localhost:3000

and

http://localhost:3001

You need to terminate both processes typing ctrl-c in your keyboard while in the terminal, so no process is keeping the 3000 port busy, and nothing appears in the browser when you try to open http://localhost:3000

Then you can run npm run dev again and it will automatically start on port 3000, because that’s free.

Any time you make a change in your project (for example because you have to npm install some package) you’re going to terminate the dev server with ctrl-c, then you start it again.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2022-04-15T05:00:00+00:00) `npm run dev` is a long-running program. Retrieved from https://www.scien.cx/2022/04/15/npm-run-dev-is-a-long-running-program/

MLA
" » `npm run dev` is a long-running program." flaviocopes.com | Sciencx - Friday April 15, 2022, https://www.scien.cx/2022/04/15/npm-run-dev-is-a-long-running-program/
HARVARD
flaviocopes.com | Sciencx Friday April 15, 2022 » `npm run dev` is a long-running program., viewed ,<https://www.scien.cx/2022/04/15/npm-run-dev-is-a-long-running-program/>
VANCOUVER
flaviocopes.com | Sciencx - » `npm run dev` is a long-running program. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/15/npm-run-dev-is-a-long-running-program/
CHICAGO
" » `npm run dev` is a long-running program." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2022/04/15/npm-run-dev-is-a-long-running-program/
IEEE
" » `npm run dev` is a long-running program." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2022/04/15/npm-run-dev-is-a-long-running-program/. [Accessed: ]
rf:citation
» `npm run dev` is a long-running program | flaviocopes.com | Sciencx | https://www.scien.cx/2022/04/15/npm-run-dev-is-a-long-running-program/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.