最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to restart node server on file change - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Aug 22, 2015 at 16:33 Jeanluca Scaljeri asked Aug 22, 2015 at 12:08 Jeanluca ScaljeriJeanluca Scaljeri 29.1k66 gold badges232 silver badges379 bronze badges 2
  • github./remy/nodemon – vp_arth Commented Aug 22, 2015 at 12:13
  • You can use nodemon - github./remy/nodemon or forever - github./foreverjs/forever – Krzysztof Safjanowski Commented Aug 22, 2015 at 12:14
Add a ment  | 

6 Answers 6

Reset to default 7

use this:

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

and remove server.close code.

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./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.

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.

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

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

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

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

发布评论

评论列表(0)

  1. 暂无评论