Jeanluca Scaljeri
Jeanluca Scaljeri

Reputation: 29139

How to restart node server on file change

I'm developing a node/express app

$> ./node_modules/babel/bin/babel-node.js index.js

Now I would like to reload the application if I make changes to index.js or any other dependency. How can I do this. I guess I have to use gulp for this, but than still I would like to have some advice on how to do this (which modules to use ect)

UPDATE: I've just tested with supervisor, but when something changes I get the following error:

$> /node_modules/.bin/supervisor --exec ./node_modules/babel/bin/babel-node.js index.js 

crashing child
Starting child process with './node_modules/babel/bin/babel-node.js     index.js'
events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: listen EADDRINUSE
    at exports._errnoException (util.js:746:11)
    at Server._listen2 (net.js:1146:14)
    at listen (net.js:1172:10)
    at Server.listen (net.js:1257:5)

UPDATE: I just tried nodemon but I get the same errors as with supervisor:

$> nodemon  --exec ./node_modules/babel/bin/babel-node.js index.js  --watch libs

...
22 Aug 16:58:35 - [nodemon] restarting due to changes...
22 Aug 16:58:35 - [nodemon] starting `./node_modules/babel/bin/babel-   node.js index.js`
events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: listen EADDRINUSE
    at exports._errnoException (util.js:746:11)
    at Server._listen2 (net.js:1146:14)
    at listen (net.js:1172:10)

UPDATE: I've solved the EADDRINUSE issue by adding the following to index.js

process.on('exit', () => {
    server.close();
})

process.on('uncaughtException', () => {
    server.close();

})

process.on('SIGTERM', () => {
    server.close();
})

However, now it seems to restart, but the new code is not loaded

Upvotes: 4

Views: 5712

Answers (6)

朱阳星
朱阳星

Reputation: 11

use this supervisor -- -r 'babel-register' index.js because Error: Cannot find module 'babel/re‌​gister'. after checked the modules ,i thought it changed to babel-register and it works for me

Upvotes: 1

kzzzf
kzzzf

Reputation: 2664

I was really disappointed with the performance of all the solutions where you run babel-node within nodemon (or supervisor). So I built this:

https://github.com/kmagiera/babel-watch

You can use it as follows (perhaps in your package.json scripts section):

babel-watch -w src src/main.js

The difference is that instead of restarting whole babel-node process on every file change (which takes like 1.5sec on my MBP) it runs babel in parent process and starts "pure" node process with all transpiled JS files provided at your script startup.

Upvotes: 3

Jhen-Jie Hong
Jhen-Jie Hong

Reputation: 71

use this:

supervisor -- -r 'babel/register' index.js

and remove server.close code.

Upvotes: 7

cesarluis
cesarluis

Reputation: 907

The most popular tools for that purpose are nodemon, forever and supervisor. You can install them via npm. For other tasks like css pre-processors, minification, tests run etc. You can use task managers like Grunt or Gulp

Upvotes: 1

Todd Price
Todd Price

Reputation: 2760

Use nodemon:

Install it globally:

npm install -g nodemon

Use it on your project:

nodemon myscript.js

It will watch for changes in your project directory and restart the script when it sees them.

Upvotes: 2

Daveman
Daveman

Reputation: 1096

There are a lot of tools to do this. Take a look at this post: Restart node upon changing a file

Maby the most common is Supervisor: https://github.com/petruisfan/node-supervisor

Upvotes: 1

Related Questions