简体   繁体   中英

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

I just started using nodemon and strtaed creating a very small example project. However, as soon as I launch the server sudo npm run dev I get the following error:

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

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

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

dashboard@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] watching extensions: js,mjs,json [nodemon] starting 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/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...

So below is where the error should be:

app.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

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

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

module.exports = router;

users.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

{
    "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

2) This one too but no success

3) I consulted this source

4) I came across this too but could still not figure out what could be wrong

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:

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

These should be defined in app.js where you have this definition const app = express(); . They will then apply for all other routes in your Express application, so defining them multiple times is not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM