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

前端 未结 30 2071
闹比i
闹比i 2020-11-28 17:24

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 loc

相关标签:
30条回答
  • 2020-11-28 17:53

    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
    
    0 讨论(0)
  • 2020-11-28 17:53

    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

    0 讨论(0)
  • 2020-11-28 17:55

    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.

    0 讨论(0)
  • 2020-11-28 17:55

    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"
    },
    
    0 讨论(0)
  • 2020-11-28 17:55

    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.

    0 讨论(0)
  • 2020-11-28 17:57

    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
    
    0 讨论(0)
提交回复
热议问题