AI Workshop: learn to build apps with AI →
npm: `npm run dev` is a long-running program

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


Each time I run npm run dev I get a different localhost port (3000, then 3001, then 3002). How do I force it to use port 3000?

When working on a site locally, you run 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 get the server running on port 3001:

So now you have the same app running on

http://localhost:3000

and

http://localhost:3001

Stop both processes with Ctrl+C in each terminal so that port 3000 is free. Then run npm run dev again and it will use port 3000.

Whenever you need to restart (for example after npm install), stop the dev server with Ctrl+C and run npm run dev again.

Lessons in this unit:

0: Introduction
1: How to use or execute a package installed using npm
2: npm dependencies and devDependencies
3: How to fix the "Missing write access" error when using npm
4: npm can install packages in the parent folder
5: Install an older version of an npm package
6: Find the installed version of an npm package
7: How to test an npm package locally
8: npm global or local packages
9: What are peer dependencies in a Node module?
10: ▶︎ `npm run dev` is a long-running program
11: Semantic Versioning using npm
12: Uninstalling npm packages with `npm uninstall`
13: An introduction to the npm package manager
14: The npx Node Package Runner
15: The package.json guide
16: The package-lock.json file
17: What is pnpm?
18: Should you commit the node_modules folder to Git?
19: Update all the Node dependencies to their latest version
20: Where does npm install the packages?
21: Bumping Node.js dependencies
22: Run package.json scripts upon any file changes in a folder