Nodemon Server won't restart

Viewed 958

The Nodemon is not restarting every time if there is any changes in the file, I need to stop the server with Ctrl-C and start the server again. I'm on Windows 64bit, I have changed in line 30, yet the server is not restarting. Why is it not restarting?

const express = require('express');
const bodyparser =require('body-parser')

const app = express();
app.use(bodyparser.json());


const database = {
    users:[
        {
            id:'123',
            name:'john',
            email:'john@gmail.com',
            password:'cookies',
            entries:0,
            joined: new Date()
        },
        {
            id:'124',
            name:'sally',
            email:'sally@gmail.com',
            password:'bananas',
            entries:0,
            joined: new Date()
        }
    ]
}

app.get('/',(req,res)=>{
    res.json(database.users);
})


app.post('/signin',(req,res)=>{
    if(req.body.email === database.users[0].email && req.body.password === database.users[0].password){
        res.json('success');
    }
    else{
        res.status(400).json('error logging in');
    }

})


app.post('/register',(req,res)=>{
    const {name , email , password} = req.body;
    database.users.push({
            id:'125',
            name:name,
            email:email,
            password:password,
            entries:0,
            joined: new Date()

    })
    res.json(database.users[database.users.length-1])
})


app.listen(3000,()=>{
    console.log("listening to port 3000");
})

this is my package.json

{
  "name": "Backend-brain-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon server.js"
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "ts-node": "^8.8.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.2"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

If I define a start script inside package.json it gives me a whole new error:

> Backend-brain-api@1.0.0 start D:\Nitin\Node.js & Express.js\Backend-brain-api
> nodemon server.js

'Express.js\Backend-brain-api\node_modules\.bin\' is not recognized as an internal or external command,
operable program or batch file.
internal/modules/cjs/loader.js:985
  throw err;
  ^

Error: Cannot find module 'D:\Nitin\nodemon\bin\nodemon.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! Backend-brain-api@1.0.0 start: `nodemon server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the Backend-brain-api@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2 Answers

It was a Windows path thing. Windows was not able to get the path as it had unnecessary special characters like the & and the whitespaces in the path names. Looks like Node is not able to resolve the paths.

Instead of typing nodemon server.js in your terminal, type npm start. This should execute the start script in your package.json and hence run nodemon on server.js.

Hope this helps

Related