Javascript 在脚本inlcude上: "start": "nodemon app.js --delay 1500ms"

Javascript 在脚本inlcude上: "start": "nodemon app.js --delay 1500ms",javascript,node.js,npm,port,Javascript,Node.js,Npm,Port,我相信问题是在我看来,旧端口没有被nodemon及时关闭以重新启动。我在使用multer时遇到了这个问题 对于windows用户,只需在任务中简单地停止Node.js的所有进程 经理 希望对您有所帮助可能在两个位置添加服务器或应用程序listen()方法。 在中为应用程序启动搜索listen()方法 这就是为什么它作为服务器从端口XXXX开始返回,并且端口XXXX已经在使用,消息在linux中并排出现 打开你的终端 从进程中释放端口 ->kill$(lsof-t-i:$port) 如果只想关闭

我相信问题是在我看来,旧端口没有被nodemon及时关闭以重新启动。我在使用multer时遇到了这个问题

对于windows用户,只需在任务中简单地停止Node.js的所有进程 经理

希望对您有所帮助

可能在两个位置添加服务器或应用程序listen()方法。 在中为应用程序启动搜索listen()方法 这就是为什么它作为服务器从端口XXXX开始返回,并且端口XXXX已经在使用,消息在linux中并排出现

  • 打开你的终端
  • 从进程中释放端口 ->kill$(lsof-t-i:$port)

如果只想关闭一个端口,只需运行此命令。
kill-9$(lsof-t-i:3000)

pkill
kill
之间的区别在于有人处理粘土。在kill中,应用过滤器。你只要停止你想要的端口

pkill
命令关闭所有节点进程。
pkill-9节点

使用pkill可以避免开发过程中偶尔发生的内存泄漏。如果有多个节点,它将杀死所有节点

package.json中脚本的使用也是一个例子

"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"
},

终止拥有端口3000的进程

首先,让我们看看如何杀死一个端口打开的进程

使用lsof命令,我们可以检索具有给定端口的PID:

$ lsof -i :3000 -t
12345
然后我们可以通过执行以下操作来终止此过程:

$ kill 12345
让我们把它变成一行:

lsof -i 3000 -t | xargs kill
如果您使用环境变量设置服务器端口,我们可以指定,而不是硬编码我们的值:

lsof -i ${PORT} -t | xargs kill
最后,如果未设置环境变量,我们可以默认为端口3000:

lsof -i ${PORT:-3000} -t | xargs kill
让nodemon执行钩子

Nodemon允许您通过Nodemon.json配置文件设置事件挂钩:

{
  "events": {
    "crash": "sh -c 'lsof -i :${PORT:-3000} -t | xargs kill'"
  }
}
这将导致nodemon在应用程序崩溃时执行sh-c'lsof-i:
${PORT:-3000}-t|xargs
kill命令,从而杀死它生成的保持端口打开的子进程

或者你可以试试这个

fuser -k PORT-NO/tcp
例如:

你也可以试试这个

fuser -n tcp -k PORT-NO
例如:


我在NodeJS上使用带有nodemon的express server。 我收到以下消息,似乎是个错误:

$ node ./bin/www
Port 3000 is already in use
有一种通用解决方案,如果终止所有节点服务器连接,可以在package.json文件中添加以下代码:

"scripts": {
    "start": "node ./bin/www",
    "stop": "taskkill -f -im node.exe"
},
此外,我在Win10 x64上找到了几种windows命令和bash解决方案

我所有的笔记都在这里:

netstat -ano | findstr :3000
tskill typeyourPIDhere 
$ netstat -ano | find "14228"
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
$ tasklist | find "node"
$ 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

#终止所有NodeJS服务器连接

$ 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.
>> Command Line
$ netstat /?
$ netstat -a -n -o
$ netstat -ano

#示例:打开Windows任务管理器并查看Windows上的“node.exe”PID编号

$ 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.
>> Command Line
$ netstat /?
$ netstat -a -n -o
$ netstat -ano

#按端口号在Windows中终止进程(示例)

如需帮助:

netstat -ano | findstr :3000
tskill typeyourPIDhere 
$ netstat -ano | find "14228"
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
$ tasklist | find "node"
$ 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
代码1:

netstat -ano | findstr :3000
tskill typeyourPIDhere 
$ netstat -ano | find "14228"
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
$ tasklist | find "node"
$ 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
代码2:

netstat -ano | findstr :3000
tskill typeyourPIDhere 
$ netstat -ano | find "14228"
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
$ tasklist | find "node"
$ 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
代码3:

netstat -ano | findstr :3000
tskill typeyourPIDhere 
$ netstat -ano | find "14228"
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
$ tasklist | find "node"
$ 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

#查看特定端口的命令行

在cmd中:

netstat -ano | findstr :3000
tskill typeyourPIDhere 
$ netstat -ano | find "14228"
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
$ tasklist | find "node"
$ 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
在bash中:

netstat -ano | findstr :3000
tskill typeyourPIDhere 
$ netstat -ano | find "14228"
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
$ tasklist | find "node"
$ 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

#使用“任务列表”命令查找node.exe

在cmd中:

netstat -ano | findstr :3000
tskill typeyourPIDhere 
$ netstat -ano | find "14228"
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
$ tasklist | find "node"
$ 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
在bash中:

netstat -ano | findstr :3000
tskill typeyourPIDhere 
$ netstat -ano | find "14228"
$ netstat -ano | grep "14228" or $ netstat -ano | grep 14228
$ tasklist | find "node"
$ 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

在我的情况下,我刚刚开始使用VS代码,并遵循使用Sequelize的教程。 最后,我得到了一个bin/www文件,其中包含listen()。 我不知道这一点,我通过运行node app.js来运行我的应用程序,当它不起作用时,我用.listen()在express server中添加了一些东西(效果很好)

但当开始使用nodemon和VSCode时,它指向bin/www,这需要我的app.js


长话短说,我在app.js中添加了.listen(),并在不应该添加app.js的时候直接运行app.js,然后在ubuntu中运行bin/www.

,首先使用端口号:sudo lsof-I:3000抓取进程,然后使用kill命令终止进程,例如,如果进程PID为4493,则使用命令:kill 4493,对于mac或windows,查找相关命令


也许你可以把这个作为参考。这个命令行可以终止在给定端口上运行的进程

npx kill-port 3000


要杀死多个端口

npx kill-port 3000 8080 4200

在运行nodemon之前,请先启动mongod。您将永远不会遇到此错误。:)

我也遇到了同样的问题。解决此问题的最佳方法是(对于windows):

  • 转到任务管理器

  • 滚动并找到名为的任务进程。 Node.js:服务器端JavaScript

  • 结束此特定任务

  • 好了!
    现在执行npm启动,它将像以前一样工作

    我已经解决了这个问题,因为MongoDB或者你之前在这个端口上运行过的另一个应用程序, 因此,要解决它,请从任务管理器中删除流程,
    或者只需将端口号从3000更改为任何其他端口。

    这非常简单。您可以通过两个简单的步骤来修复它

  • 如果存在名为“PORT”的键/条目,请检查环境变量
  • 如果找到,请删除该条目或将其重命名为其他条目
  • 事实证明,其他一些程序正在使用该变量。通常,当您启动react脚本时,它将查找具有该标题端口的环境变量

     killall -9 node
    
    当您远程开发时,上面的命令可以退出vs code ssh连接,并终止可能导致问题的所有节点进程,特别是当您在生产中使用node时,有一种更好的方法,使用netstat获取所有节点进程及其使用的端口,然后通过PID杀死唯一需要的节点进程

     netstat -lntp | grep node
    
    您将获得所有节点进程

     tcp6  0      0 :::5744    :::*    LISTEN     3864/node
    
    然后,当您得到PID(3864)时,只需通过PID终止进程

     kill -HUP PID 
    

    最近我在win10上遇到了一个奇怪的
    端口问题
    ,无法启动服务器进程侦听端口8080,我将其更改为18080,然后