简体   繁体   中英

nodejs : nodemon app crashed - waiting for file changes before starting

I was actually following a nodejs course on Udemy and suddenly down the course, the code started breaking and giving some errors. I then tried copying the instructor's code, but still, the problem was the same.

I also followed this answer on StackOverflow itself but the problem remains the same.

I am attaching all the js files below and also the error message from the terminal below:

Error message from the terminal

This is what the file structure looks like

Now the js files:

app.js file:

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
 
const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');
const errorController = require('./controllers/error');
 
const app = express();
 
app.set('view engine', 'ejs');
app.set('views', 'views');
 
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.static(path.join(__dirname, 'public')));
 
app.use('/admin', adminRoutes);
app.use(shopRoutes);
 
app.use(errorController.get404Page);
 
app.listen(3000);

controllers/error.js file:

exports.get404 = (req, res, next) => {
  res.status(404).render('404', { pageTitle: 'Page Not Found' });
};

controllers/products.js file

const Product = require('../models/product');
 
exports.getAddProduct = (req, res, next) => {
  res.render('add-product', {
    pageTitle: 'Add Product',
    path: '/admin/add-product',
    formsCSS: true,
    productCSS: true,
    activeAddProduct: true
  });
};
 
exports.postAddProduct = (req, res, next) => {
  const product = new Product(req.body.title);
  product.save();
  res.redirect('/');
};
 
exports.getProducts = (req, res, next) => {
  Product.fetchAll(products => {
    res.render('shop', {
      prods: products,
      pageTitle: 'Shop',
      path: '/',
      hasProducts: products.length > 0,
      activeShop: true,
      productCSS: true
    });
  });
};

models/product.js file:

const fs = require('fs');
const path = require('path');
 
const p = path.join(
  path.dirname(process.main.filename),
  'data',
  'products.json'
);
 
const getProductsFromFile = cb => {
  fs.readFile(p, (err, fileContent) => {
    if (err) {
      cb([]);
    } else {
      cb(JSON.parse(fileContent));
    }
  });
};
 
module.exports = class Product {
  constructor(t) {
    this.title = t;
  }
 
  save() {
    getProductsFromFile(products => {
      products.push(this);
      fs.writeFile(p, JSON.stringify(products), err => {
        console.log(err);
      });
    });
  }
 
  static fetchAll(cb) {
    getProductsFromFile(cb);
  }
};

routes/admin.js file:

const path = require('path');
 
const express = require('express');
 
const productsController = require('../controllers/products');
 
const router = express.Router();
 
// /admin/add-product => GET
router.get('/add-product', productsController.getAddProduct);
 
// /admin/add-product => POST
router.post('/add-product', productsController.postAddProduct);
 
module.exports = router;

routes/shop.js file:

const path = require('path');
 
const express = require('express');
 
const productsController = require('../controllers/products');
 
const router = express.Router();
 
router.get('/', productsController.getProducts);
 
module.exports = router;
util/path.js file:

const path = require('path');
 
module.exports = path.dirname(process.main.filename);

I am new to backend development, nodejs to be specific. Please help me find my mistake.

In many file you have used process.main.filename , but there is no property named process.name for node.js process, so process.main is undefined . As a result reading the property process.main.filename gives the error.

So, to solve your problem:

Replace every process.main.filename with process.mainModule.filename OR require.main.filename

(there is difference in both, but it will not matter in your app.)

and see if it works. I guess this is a mistake on your part while writing the code.


Tip: Always try to understand the error using terminal output. That way You can solve most of the problems.

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