NODEMON -- 应用程序崩溃 - 在启动之前等待文件更改

NODEMON -- app crashed - waiting for file changes before starting

I'm following this tutorial.

当我运行npm run server时,如视频中的13:10,我得到错误:

[nodemon] app crashed - waiting for file changes before starting...

为什么会这样?端口 5000 是否已被使用?


这是终端:

#########:MERN_SHOPPING_LIST #######$ npm run server

mern_shopping_list@1.0.0 server /Users/MyName/Documents/Web Dev/MERN_SHOPPING_LIST
nodemon server.js
[nodemon] 1.18.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
/Users/MyName/Documents/Web Dev/MERN_SHOPPING_LIST/server.js:1
(function (exports, require, module, __filename, __dirname) { const express = required('express');
                                                                              ^
ReferenceError: required is not defined
    at Object.<anonymous> (/Users/MyName/Documents/Web Dev/MERN_SHOPPING_LIST/server.js:1:79)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
[nodemon] app crashed - waiting for file changes before starting...

你用了一个词 required 而是 require

代码应为 server.js file 第 1 行的 require('express');

nodemon 会在您遇到错误时尝试重新启动程序。如果您进行更改或进行必要的更正,您 将不会 需要再次 运行 命令 node server.js

简单的答案是:

转到 Mongodb 集群并编辑 IP 白名单 并刷新它

然后重新连接问题解决。

Happy Coding

这是一个非常常见的错误,当您使用 nodemon 启动服务器时,您可能会遇到此错误。那么,这是怎么回事?以及解决方案是什么?

错误原因及解决方案:

第一个:

也许您的 PC 运行 后台有多个进程。所以你需要停止所有 运行 的节点进程。

快速技巧,通过 运行 在终端上将它们全部杀死:

pkill -f node

或者杀死一个特定的端口而不是所有

sudo lsof -i :8000 //replace 3000 with your port number
sudo kill -9 51009 // replace 31363 with your PID

然后重启nodemon。

第二个:

Server.js 和 package.json 不在同一个文件夹中。

//check package.json
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
     "start": "nodemon Server.js"
 }

第三个:

您在任何文件中编写了错误的代码。因此,请确保您没有错过任何 JS 语法。如果你写错了代码,这里我说的是语法错误(不是逻辑错误),那么你的nodemon将无法启动。

打错了。

首先,这是什么(function (exports, require, module, __filename, __dirname) { ... })?这是模块包装函数。抛出异常时,模块已经有了它的包装函数。

让我们来看看错误。

1 > const express = required('express');
                           ^

那是你的错字所在。

现在让我们进入[nodemon]

您从“[nodemon]”收到错误消息的原因是您正在为服务器使用名为 nodemon 的开发工具。

问题可能出在文件夹中。

我按文件夹更改了所有存档,我无法使用 npm 运行 dev.

看看存档和其他人,如果有人在原始文件夹之外可能是错误的

问题不是 npm 运行 dev 或 nodemon