[javascript] Node.js Port 3000 already in use but it actually isn't?

I have been working with a node.js project for a few weeks and it has been working great. Usually, I use npm start to run my app and view it in a browser on localhost, port 3000.

Today, I started to get the following error while using npm start:

Server started on port 3000                                                                                                                                                                                         
Port 3000 is already in use 

I have checked the resource monitor and I have no other process running on port 3000. Why would I be getting this error message?

In my app.js I have the following code to set the port...is this incorrect? It worked fine before so I'm not sure what I am doing wrong.

// Set Port
app.set('port', (process.env.PORT || 3000));
app.listen(app.get('port'), function() {
    console.log('Server started on port '+app.get('port'));
});

Thanks for the help!


EDIT:

I have tried running netstat and TCPView to check what process is using the port, but there is nothing using that port. I also tried restarting my laptop but I still get the same error.

This question is related to javascript node.js npm port

The answer is


For windows user, Just simple stop all processes of Node.js in Task Manager

Hope it will help


if you are using webstorm just make sure your default port is not 3000 from file -> settings -> Build, Execution, Deployment -> Debugger And there change

Built-in server port

and set it to "63342" or see this answer Change WebStorm LiveEdit Port (63342)


You can search on how to kill that process.

For Linux/Mac OS search (sudo) run this in the terminal:

$ lsof -i tcp:3000
$ kill -9 PID

On Windows:

netstat -ano | findstr :3000
tskill typeyourPIDhere 

change tskill for taskkill in git bash


It's very simple. You can fix it in 2 easy steps.

  1. Check your environment variables if there is a key/entry with name "PORT".
  2. If found delete that entry or rename it to something else.

It turns out that some other program is using that variable. Usually when you start react-scripts it will look for an environment variable with that title PORT.


server or app listen() methods might be added at 2 places. Search for listen() methods in the for the application startups thats why its returning as Server started at Port XXXX and Port XXXX already in use message coming side by side


check for any process running on the same port by entering the command:

sudo ps -ef

You can find the process running on the respective node port, then kill the node by

kill -9 <node id>

If the problem still remains then just kill all the node

killall node

This happens to me sometimes, EADDR in use. Typically there is a terminal window hiding out in the background that is still running the app. You can stop process with ctrl+C in the terminal window.

Or, perhaps you are listening to the port multiple times due to copy/pasta =)


I got this problem using Git Bash on Windows. I run npm start, or node app.js. After terminating it with Ctrl+C shortly and trying to start the server again using npm start or node app.js then I get this error message.

When I do this with the regular Windows Command Prompt, however, it works fine.

Or You can do it in another way. Open the Task Manager and Find the "Node.js:Server-side JavaScript" row. Select that and end task. It should work now.

Thanks.


Sometimes it happens, as @sova proposed This happens to me sometimes, EADDR in use. Typically there is a terminal window hiding out in the background that is still running the app. And that's also right with me.

It happens, when you have opened terminal for long time, yeah you have right, you have stop the process. But sometimes it didn't stop in the background. So best solution is that you close the terminal and start it again. It will solves your problem. becuase in my case it works.

Also,

sudo lsof -i:<PORT_NO>

close the instance for present time but unable to stop the process in background. So for one time,

sudo kill <PID>

works, but again when we update our code and save, this problem occurs again as with Nodemon.

So exit the terminal will solve the problem. OR

  killall -9 node

You can use kill-port. In firstly, kill exist port and in secondly create server and listen.

const kill = require('kill-port')

kill(port, 'tcp')
    .then((d) => {
        /**
     * Create HTTP server.
     */
        server = http.createServer(app);

        server.listen(port, () => {
            console.log(`api running on port:${port}`);
        });
    })
    .catch((e) => {
        console.error(e);
    }) 

I've seen the same thing and tried all the suggestions above without success. Here are steps that resolve it for me: - turn off wifi - npm start (this should work) - turn on wifi

I'm not exactly sure what the root issue is but that resolved it for me.


Simple in linux

  • Open your terminal
  • Free port from processes -> kill $(lsof -t -i:$port)

Killing a process that owns port 3000

First, let’s take a look at how we can kill a process that has a port open.

Using the lsof command, we can retrieve the PID that has the given port:

$ lsof -i :3000 -t
12345

Then we can kill this process just by doing:

$ kill 12345

Let’s turn this into a one-liner:

lsof -i 3000 -t | xargs kill

If you’re using an environment variable to set the server port, we can specify that instead of hardcoding our values:

lsof -i ${PORT} -t | xargs kill

Lastly, we can default to port 3000 if the environment variable isn’t set:

lsof -i ${PORT:-3000} -t | xargs kill

Getting nodemon to execute hooks

Nodemon lets you set up event hooks through nodemon.json configuration file:

{
  "events": {
    "crash": "sh -c 'lsof -i :${PORT:-3000} -t | xargs kill'"
  }
}

This will cause nodemon to execute sh -c 'lsof -i :${PORT:-3000} -t | xargs kill command whenever your app crashes, thereby killing the child process it spawned that’s keeping the port open.

or you can try this one

fuser -k PORT-NO/tcp

eg:

fuser -k 3000/tcp

I also encountered the same issue. The best way to resolve is (for windows):

  1. Go to the Task Manager.

  2. Scroll and find a task process named. Node.js: Server-side JavaScript Image added for reference

  3. End this particular task.

There you go! Now do npm start and it will work as before!


Before running nodemon, Please start mongod first. You will never get this error. :)


Kills all the running ports (mac):

killall node

I had the same problem. (The below steps work fine on Windows 10):

  1. Open Task manager (press Ctrl+Alt+Delete)
  2. Select the 'Processes tab'
  3. Search for 'Node.js: Server-side JavaScript'
  4. Select it and click on 'End task' button

Now you can run npm start.

Hope it helps you.


In my circumstance I had just started using VS Code and had followed a tutorial using Sequelize. In the end I had a bin/www file that had the listen() in there. I didn't know about this and I was running my app by running node app.js, when it didn't work I then added in the express server stuff with .listen() (which worked fine).

But when starting to use nodemon and VSCode it was pointed at bin/www and that required my app.js.

Long story short I had added .listen() to my app.js and was running app.js directly when I should have not added that and run bin/www.


Open Task Manager (press Ctrl+Alt+Del Select the 'Processes Tab' Search for 'Node.js: Server-side JavaScript' Select it and click on 'End task' button


I was using express server with nodemon on NodeJS. I got the following message and it seems an error:

$ node ./bin/www
Port 3000 is already in use

There is a general solution that if you terminate all node server connections, you can add this code in your package.json file:

"scripts": {
    "start": "node ./bin/www",
    "stop": "taskkill -f -im node.exe"
},

In addition, I've found several solutions windows command and bash on Win 10 x64.

All my notes are here:


# Terminate all NodeJS Server Connections

$ taskkill -f -im node.exe
SUCCESS: The process "node.exe" with PID 14380 has been terminated.
SUCCESS: The process "node.exe" with PID 18364 has been terminated.
SUCCESS: The process "node.exe" with PID 18656 has been terminated.

# Example: Open the Windows Task Manager and see "node.exe" PID number on Windows

>> Command Line
$ netstat /?
$ netstat -a -n -o
$ netstat -ano

# Kill a process in Windows by Port Number (Example)

For Help:

$ taskkill /?
$ tskill /?

Code 1:

$ taskkill -pid 14228
ERROR: The process with PID 14228 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).

Code 2:

$ taskkill -f -pid 14228
SUCCESS: The process with PID 14228 has been terminated.

Code 3:

$ tskill 14228

# Command line for looking at specific port

in cmd:

$ netstat -ano | find "14228"

in bash:

$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228

# Find node.exe using "tasklist" command

in cmd:

$ tasklist | find "node"

in bash:

$ tasklist | grep node
$ tasklist | grep node.exe
node.exe                     14228 Console                    2     48,156 K
node.exe                     15236 Console                    2     24,776 K
node.exe                     19364 Console                    2     24,428 K

Maybe you can take this as reference. This single command line can kill the process running on given port.

npx kill-port 3000

enter image description here


To kill multiple ports.

npx kill-port 3000 8080 4200

This started happening to me today on Windows. I have restarted my computer and checked that nothing is on the port 3000 and it isn't.

I tried using 3001, 3005 with the same result.

In the end I moved to 8881 and it works now.

The only thing that changed for me was installing windows updates and updating source-map-explorer. As this also occurs in other apps its either something with Web Storm or windows. My guess is the ports may be locked down but the ones in the 88XX range are not.


For windows, The Task Manager would definitely show a node process running. Try to kill the process, it will solve the problem.


For windows users, you can use CurrPorts tool to kill ports under usage easily

enter image description here


I have solved this problem because the MongoDB or there is another app u had run it before on this port, so to solve it kill the process from task manager, or just change the number of the port from 3000 to any other one.


If you want to close only one port, just run this command. kill -9 $(lsof -t -i:3000)

The difference between pkill and kill is someone process clay. In kill you apply a filter. you just stop the port you want.

The pkill command closes all node processes. pkill -9 node

Use pkill to avoid memory leaks that occur occasionally during development. if there is more than one node, it kills them all.

The use of scripts in package.json is also exemplified.

"scripts": {
    "server:start": "cd server && yarn start",
    "server:restart": "cd server && yarn restart",
    "frontend:start": "cd frontend && yarn start",
    "frontend:restart": "kill -9 $(lsof -t -i:4200) && yarn start:frontend"
},
"scripts": {
    "start": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/index.ts",
    "restart": "pkill -9 node && start",
    "kill": "pkill -9 node"
},

In ubuntu first grab the process by using port number: sudo lsof -i:3000 then use kill command to kill the process, for example if the process PID is 4493 the use command: kill 4493 , for mac or windows find the related command

enter image description here


I have spent 2h on finding out why EADDRINUSE wasn't allowing me to sart an app (other node-express servers were ok)... it started working after adding lazyConnect: true, to datasource configuration.

Don't ask me why it helped. I do not know. I am putting this info here just for people having the same issue.


Try opening the localhost in your browser. Just type: localhost:3000 in the address bar.

If the app opens-up, it means your previous npm run is still active. Now, you can just make changes to the code and see the effects if you are designing the same app, or if you wanna run another app, just tweak the code (in index.js of previously running app) a little-bit and (probably refresh the browser tab) to make it crash ;)..... Now go run npm run start again from your new app directory. Hope this helps! :)

or

You can open the Task Manager (WINDOWS_KEY+X > Task Manager) and you'll see the "Node.js:Server-side JavaScript" row. Select that and end task....It should work now!!



If not, change the .env file of your app to include port:3002 and run the new app. This will allow you to run two separate apps on different ports. Cheers!!


In package.json scripts inlcude:

"start": "nodemon app.js --delay 1500ms"

I believe the issue was for me the time that the old port was not shutting down in time by nodemon for the restart. I experienced the issue using multer.


It may be an admin process running in the background and netstat doesn't show this.
Use tasklist | grep node to find the PID of this admin process and then kill PID


Came from Google here with a solution for High Sierra.

Something changed in the networking setup of macos and some apps (including ping) cannot resolve localhost.

Editing /etc/hosts seems like a fix:

cmd: sudo nano /etc/hosts/ content 127.0.0.1 localhost

Or simply (if you're sure your /etc/hosts is empty) sudo echo '127.0.0.1 localhost' > /etc/hosts


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to npm

What does 'x packages are looking for funding' mean when running `npm install`? error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist` ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean` What exactly is the 'react-scripts start' command? On npm install: Unhandled rejection Error: EACCES: permission denied Difference between npx and npm?

Examples related to port

Docker - Bind for 0.0.0.0:4000 failed: port is already allocated How do I kill the process currently using a port on localhost in Windows? Node.js Port 3000 already in use but it actually isn't? Can't connect to Postgresql on port 5432 Spring Boot - How to get the running port Make docker use IPv4 for port binding How to change the default port of mysql from 3306 to 3360 Open firewall port on CentOS 7 Unable to launch the IIS Express Web server, Failed to register URL, Access is denied XAMPP Port 80 in use by "Unable to open process" with PID 4