Nodemon restarts, but saved changes to index.js not reflected in output
Asked Answered
R

3

0

Here's my code for an express server:

const express=require('express');
express();

const app=express();

app.get('/',(req,res)=>{
    res.send('Welcome to API xyz!');
});

app.listen(3000,()=>{
    console.log('Listening on port 3000...');

});

Running the server from a git bash terminal, in the app's directory, using:

nodemon index.js

initially gives the message:

[nodemon] starting `node index.js`
Listening on port 3000...

Whenever I save a change to the output of res.send() as follows:

res.send('Welcome to API abc!');

and save the index.js file, I get this message:

[nodemon] restarting due to changes...

but I do not get the console.log() text, and when I reload localhost:3000 in Chrome, I still get the output:

Welcome to API xyz!

How can I get the server to update in response to saved changes without having to stop nodemon and restart it (which is the whole point of running nodemon in the first place)?

EDIT: I noticed that when nodemon restarts, I get:

restarting due to changes...

but I don't get

starting `node index.js`

after that. I only get

starting `node index.js`

when I first run nodemon.

EDIT 2: thinking that maybe this is related to the same issue that other nodemon users have experienced, as noted here in its Github issues log?

Roughandtumble answered 15/6, 2021 at 19:3 Comment(11)
It is not clear what your issue is. All the code you have posted here appears to be working as intended. You reference console.log() but the only console.log statement appears to execute properly. More details are required to help.Delagarza
@Delagarza The issue is that when I reload the browser, it doesn't show the updated text, it still says "Welcome to API xyz!" instead of the new text "Welcome to API abc!"Roughandtumble
Have you tried a hard reload to make sure there is no caching involved?Delagarza
@Delagarza just tried Ctrl+F5 to reload, got the same "Welcome to API xyz!"Roughandtumble
Try adding verbose mode to nodemon --verbose to get a better idea of if its reloading your js file. If that doesn't show anything obvious you may need to create a sample project to reproduce your issue. If you have any tooling involved it may what is causing issues.Delagarza
@Delagarza running with --verbose shows: files triggering change check: index.js matched rule: **\*.* changes after filters (before/after): 1/1 restarting due to changes index.jsRoughandtumble
Unfortunately at this point you need to show a sample project where it fails to run but the problem is most likely local to your box. Make sure you have the latest version of node/npm/nodemon and don't have a weird configuration involving containers or a situation where you are running a separate instance.Delagarza
Let us continue this discussion in chat.Roughandtumble
can you trying adding nodemon command as a script command to your package.json "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon server.js" },Gwenny
@JatinMehrotra i added that but the changes still didn't show up after nodemon restarted the appRoughandtumble
maybe nodemon as a process is unresponsidve, try restarting your computer and try againGwenny
T
0

Maybe try removing the 2nd line that says express();

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Welcome to API xyz!");
});

app.listen(3000, () => {
  console.log("Listening on port 3000...");
});
Therapy answered 15/6, 2021 at 19:34 Comment(1)
Removed it but that didn't change the behavior.Roughandtumble
N
0

I was facing the same issue and I usually start the server from VS code terminal. Before I could try the solutions given in a related question I tried running the server from the command prompt (Run as administrator) and it worked. I use the command npm run start to start the server. Hope it helps someone!

Nitwit answered 17/8, 2022 at 11:23 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Dragelin
B
0

Its easy do not use nodemon install the latest version of npm and use this command:- node --watch filename for eg node --watch index.js

It will work same as the nodemon but remeber on latest version of node I think 19 but if you are trying 17 or something it will not work so make sure to update it before using

Berman answered 11/3 at 16:57 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Dragelin

© 2022 - 2024 — McMap. All rights reserved.