Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS - nodemon not restarting my server

I installed NodeJS (4.4.5) and then proceed to install nodemon (1.9.2) as well, I follow all the instrucctions of instalation (npm install -g nodemon)

I created a new folder, inside I have my server.js with some basic code:

var http = require('http');

http.createServer(function (request, response){
    response.writeHead( 200, {'Content-Type': 'text/plain'} );
    response.write("Hello world");
    response.end();
}).listen(3000, 'localhost');

console.log('http://localhost:3000');

So when I run in my console "nodemon server.js" I get this:

[nodemon] 1.9.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
http://localhost:3000

(Which means that is running fine)

But when I make some changes in my server.js, It doesn't restart the server

What could be the issue?

like image 770
BrianCas Avatar asked Jun 03 '16 20:06

BrianCas


People also ask

How do I restart Nodemon restart?

Running non-Node code While Nodemon is running, we can manually restart our application. So instead of stopping and restarting Nodemon, we can just type rs and press enter, and Nodemon will restart the server or the running process for us.

Why Nodemon is not working?

Use npx to solve the error "'nodemon' is not recognized as an internal or external command, operable program or batch file", e.g. npx nodemon server. js or install the package globally by running npm install -g nodemon and make sure your PATH environment variable is set up correctly.

Does Nodemon restart on crash?

Nodemon monitors your source files for changes during development and reload on change. That's why it says 'waiting for file change' after a crash. In development, usually you made a mistake, correct it, save the source file, nodemon notices that and reloads.


4 Answers

In some networked environments (such as a container running nodemon reading across a mounted drive), you will need to use the legacyWatch: true which enabled Chokidar's polling.

Via the CLI, use either --legacy-watch or -L for short:

nodemon -L

like image 105
Sergey Ivchenko Avatar answered Oct 13 '22 10:10

Sergey Ivchenko


I had the same issue on nodemon 1.9.2 : 0 files were watched by nodemon

(try verbose mode nodemon server.js -V to get more information)

After some research, this issue was due to my root directory name : My root directory was named "mysite.git" thus nodemon was watching 0 file. I had renamed my root directory without the dot and nodemon now works as expected.

Remove all special characters from your root directory and nodemon should work as expected.

like image 23
MrP Avatar answered Oct 13 '22 08:10

MrP


You can try running like this nodemon -L yourapp.js, something like that:

"scripts": {
    "start": "node index.js",
    "dev": "nodemon -L index.js "
 }

And run command:

npm run dev

This worked for me!

like image 34
Alejandro Pérez Avatar answered Oct 13 '22 09:10

Alejandro Pérez


I just hit what seemed to be the same issue where I would make changes to my code and save them but nodemon wouldn't restart my server. The only way I could update was via 'rs' in the console. I ended here looking for a solution but then decided to just re-install nodemon as there was no good explanation for why it wasn't working:

npm uninstall nodemon
npm install nodemon

After reinstalling the automatic restart on code change worked.

like image 23
Kellen Preston Avatar answered Oct 13 '22 10:10

Kellen Preston