Search code examples
javascriptnode.jsmongodbnodemon

Nodemon error with mondoDB: " app crashed - waiting for file changes before starting"


Idk why this happen, i've readed the code 5x and i cant see the mistake. (im used requireDir)

enter image description here

server.js code:

//chamando 
const express = require('express');
const mongoose = require("mongoose");
const requireDir = require('require-dir');
//iniciando app
const app = express();

//iniciando o DB
mongoose.connect(
    "mongodb://localhost:27017/nodeapi",
    { useNewUrlParser: true }
);

requireDir('.src/models');

//first rote
app.get('/', (req, res) => {

    res.send('hello worlldd');
});

app.listen(3001); //porta

//NODEMON - LIVE 
/* "dev": "nodemon server.js" no package
npm run dev no terminal */

Product.js code:

const mongoose = require('mongoose');

const ProductSchema = new mongoose.Schema({
    title:{
        type: String,
        required: true,
    },

    description:{
        type: String,
        required: true,
    },

    url:{
        type: String,
        required: true,
    },

    createdAt:{
        type: Date,
        default: Date.now,
    },
});

mongoose.model('product', ProductSchema);

enter image description here

enter image description here

The server just dont run. Everything was normal before I put the "mongoose".

im used docker and robot3T too.

enter image description here

The whole error

enter image description here


Solution

  • The provided path path to requireDir() has a typo. You are including the path .src/models instead of ./src/models.

    Fixed server.js:

    //chamando 
    const express = require('express');
    const mongoose = require("mongoose");
    const requireDir = require('require-dir');
    //iniciando app
    const app = express();
    
    //iniciando o DB
    mongoose.connect(
        "mongodb://localhost:27017/nodeapi",
        { useNewUrlParser: true, useUnifiedTopology: true }
    );
    
    requireDir('./src/models');
    
    //first rote
    app.get('/', (req, res) => {
    
        res.send('hello worlldd');
    });
    
    app.listen(3001); //porta
    
    //NODEMON - LIVE 
    /* "dev": "nodemon server.js" no package
    npm run dev no terminal */