简体   繁体   English

React js - nodemon:应用程序崩溃 - 在启动之前等待文件更改

[英]React js - nodemon: app crashed - waiting for file changes before starting

I just started using nodemon and strtaed creating a very small example project.我刚开始使用nodemon和 strtaed 创建一个非常小的示例项目。 However, as soon as I launch the server sudo npm run dev I get the following error:但是,一旦我启动服务器sudo npm run dev我就会收到以下错误:

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

The error says that there is a mistake on users.js:7 app.use(expressLayouts);错误说users.js:7 app.use(expressLayouts);上有错误

emanuele@pc:~/Desktop/dashboard$ sudo npm run dev emanuele@pc:~/Desktop/dashboard$ sudo npm run dev

dashboard@1.0.0 dev /home/emanuele/Desktop/dashboard nodemon app.js仪表板@1.0.0 dev /home/emanuele/Desktop/dashboard nodemon app.js

[nodemon] 2.0.2 [nodemon] to restart at any time, enter rs [nodemon] watching dir(s): . [nodemon] 2.0.2 [nodemon] 随时重启,输入rs [nodemon] watch dir(s): . [nodemon] watching extensions: js,mjs,json [nodemon] starting node app.js /home/emanuele/Desktop/dashboard/routes/users.js:7 app.use(expressLayouts); [nodemon] 看扩展:js,mjs,json [nodemon] 起始node app.js /home/emanuele/Desktop/dashboard/routes/users.js:7 app.use(expressLayouts); ^ ^

ReferenceError: app is not defined at Object.参考错误:应用程序未在对象中定义。 (/home/emanuele/Desktop/dashboard/routes/users.js:7:1) at Module._compile (internal/modules/cjs/loader.js:955:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10) at Module.load (internal/modules/cjs/loader.js:811:32) at Function.Module._load (internal/modules/cjs/loader.js:723:14) at Module.require (internal/modules/cjs/loader.js:848:19) at require (internal/modules/cjs/helpers.js:74:18) at Object. (/home/emanuele/Desktop/dashboard/routes/users.js:7:1) 在 Module._compile (internal/modules/cjs/loader.js:955:30) 在 Object.Module._extensions..js (internal /modules/cjs/loader.js:991:10) 在 Module.load (internal/modules/cjs/loader.js:811:32) 在 Function.Module._load (internal/modules/cjs/loader.js:723) :14) at Module.require (internal/modules/cjs/loader.js:848:19) at require (internal/modules/cjs/helpers.js:74:18) at Object。 (/home/emanuele/Desktop/dashboard/app.js:7:19) at Module._compile (internal/modules/cjs/loader.js:955:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10) [nodemon] app crashed - waiting for file changes before starting... (/home/emanuele/Desktop/dashboard/app.js:7:19) at Module._compile (internal/modules/cjs/loader.js:955:30) at Object.Module._extensions..js (internal/modules) /cjs/loader.js:991:10) [nodemon] 应用程序崩溃 - 在开始之前等待文件更改...

So below is where the error should be:所以下面是错误应该在哪里:

app.js应用程序.js

const express = require('express');
const expressLayouts = require('express-ejs-layouts');

const app = express();

app.use(expressLayouts);
app.set('view engine', 'ejs');

// Routes
app.use('/', require('./routes/index'));
app.use('/users', require('./routes/users'));

const PORT = process.env.PORT || 5000;

app.listen(PORT, console.log(`Server started on port ${PORT}`));

index.js索引.js

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => res.render('welcome'));

module.exports = router;

users.js用户.js

const express = require('express');
const router = express.Router();

// Login Page
router.get('/login', (req, res) => res.send('Login'));

// Register Page
router.get('/register', (req, res) => res.send('Register'));

module.exports = router;

If needed below my package.json如果需要在我的package.json下面

{
    "name": "dashboard",
    "version": "1.0.0",
    "description": "",
    "main": "app.js",
    "scripts": {
        "start": "node app.js",
        "dev": "nodemon app.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "bcryptjs": "^2.4.3",
        "connect-flash": "^0.1.1",
        "ejs": "^3.0.1",
        "express": "^4.17.1",
        "express-ejs-layouts": "^2.5.0",
        "express-session": "^1.17.0",
        "mongoose": "^5.8.11",
        "passport": "^0.4.1",
        "passport-local": "^1.0.0"
    },
    "devDependencies": {
        "nodemon": "^2.0.2"
    }
}

I don't understand what is wrong in the above piece of code.我不明白上面这段代码有什么问题。 I consulted some posts such as:我查阅了一些帖子,例如:

1) This but could't solve the problem 1) 但不能解决问题

2) This one too but no success 2) 这个也是,但没有成功

3) I consulted this source 3)我咨询了这个来源

4) I came across this too but could still not figure out what could be wrong 4)我也遇到过这个,但仍然无法弄清楚可能是什么问题

Thanks for pointing in the right direction to understand where the error could be.感谢您指出正确的方向以了解错误可能出在哪里。

Inside index.js (and users.js ) there is no app variable defined:index.js (和users.js )中没有定义app变量:

app.use(expressLayouts);
app.set('view engine', 'ejs');

These should be defined in app.js where you have this definition const app = express();这些应该在app.js中定义,你有这个定义const app = express(); . . They will then apply for all other routes in your Express application, so defining them multiple times is not needed.然后它们将申请 Express 应用程序中的所有其他路由,因此不需要多次定义它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ [nodemon]应用程序崩溃-等待文件更改,然后再开始...” - '[nodemon] app crashed - waiting for file changes before starting…' nodemon,应用程序崩溃,在启动前等待文件更改 - nodemon , app crashed , waiting for file changes before starting nodemon_app 崩溃 - 在开始之前等待文件更改 - nodemon_app crashed - waiting for file changes before starting mondoDB 的 Nodemon 错误:“应用程序崩溃 - 启动前等待文件更改” - Nodemon error with mondoDB: “ app crashed - waiting for file changes before starting” nodemon 应用程序崩溃 - 在开始之前等待文件更改? - nodemon app crashed - waiting for file changes before starting? nodejs:nodemon应用程序崩溃-在启动前等待文件更改 - nodejs : nodemon app crashed - waiting for file changes before starting 节点 js 错误:[nodemon] 应用程序崩溃 - 启动前等待文件更改 - Node js error: [nodemon] app crashed - waiting for file changes before starting nodemon 应用程序崩溃 - 在从服务器获得响应后开始错误之前等待文件更改 - nodemon app crashed - waiting for file changes before starting error after geting response from server nodemon 应用程序崩溃 - 在启动前等待文件更改。 有人可以解决这个问题吗 - nodemon app crashed - waiting for file changes before starting. Can someone sort this out please 当我运行 nodemon 服务器时,我收到错误消息“错误的身份验证失败。[nodemon] 应用程序崩溃 - 在开始之前等待文件更改......” - When I run nodemon server I get the error "bad auth Authentication failed. [nodemon] app crashed - waiting for file changes before starting..."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM