自动检测代码变化并重启nodejs工具使用对比

本文对比分析了 Node.js 开发中常用的自动化代码热更新工具,包括 forever、nodemon、node-supervisor 和 node-dev,详细介绍了它们的功能、使用方法和性能表现,帮助开发者根据需求选择合适的工具,提高开发效率。
摘要由CSDN通过智能技术生成

As a newbie Node.js developer coming from a Java and PHP background, I found the process of having to search for the correct node terminal and hitting

[Ctrl-C] [Up-Arrow] [Enter]

after every code change to be very archaic. Luckily there are many tools available that can automate this process for you, saving you valuable time and unnecessary pain. These seconds can really add up if you are constantly making a lot of little changes to your code. With all that time you’re saving, you can unglue yourself from your seat and go out for a walk or take an extra coffee break.

 


coffee

 


forever

forever is not only a great CLI tool for keeping your app running continuously in a production environment, but also has advanced features that support running multiple node processes as background services. In this case, it gives you the option of restarting the server on file changes by using the -w flag. By using this flag, we not are only automatically restarting the application when the code changes, but also when and if the application goes down.

Installation:

Use forever -w, instead of node to start your app:

nodemon

I found out about Remy Sharp’s nodemon while browsing StackOverflow and have been using it for developing Node.js apps ever since. It’s fast and very simple to use… and it also supports CoffeeScript:

Installation:

 

Use nodemon, instead of node:

You can also specify a custom list of file extensions to watch for with the -e switch like so:

 

Alternatives


 

node-supervisor

Developed by Isaac Schlueter, the supervisor module is very similar in functionality and customizability to nodemon. However, I have seen complaints from the community that it eats up more CPU than it needs to and that it loses out on important functionality such as –debug and –harmony as it passes all arguments as arguments to the script itself rather than node. Also, unlike nodemon and forever, it will keep trying to restart the application when there is an error, which can overload your terminal with duplicate error statements.

 

Installation:

 

Use supervisor, instead of node:

Like nodemon, supervisor also allows you to specify your own list of files to watch with the -e switch like so, just without the dot:


node-dev

node-dev is great alternative to both nodemon and supervisor for developers who like to get growl (or libnotify) notifications on their desktop whenever the server restarts or when there is an error. It also supports CoffeeScript and LiveScript. To use the growl notifications you have to install Growl from http://growl.info/downloads.
It should be noted that node-dev should not be used in production — it’s not designed for hot code reloading, but for easier development.

 

Installation:

 

Use node-dev, instead of node:

Comparison of tools

I was most impressed with forever and nodemon after researching and trying out all of the various alternatives. Not only are they the most actively developed amongst the alternatives, the support from the users is overwhelming as seen by the number of GitHub stars (as of December 9, 2013):

forever: 3527
nodemon: 2331
node-supervisor: 1442
node-dev: 446

For those of you in a production environment and are already using forever to keep your processes alive, you can simply add the -w flag on your startup script instead of having to install another dependency.

For a development environment, using nodemon, supervisor, or node-dev are all great solutions to having to manually restart the server after every code change. I recommend trying out nodemon first and checking out the others if you are not satisfied with it’s performance. I was hooked after using it once: it’s fast, simple to use, and is greatly supported by the community.

Update: Many of you suggested that pm2 should also be included in this list. Although pm2 is a great tool, it no longer seems to support monitoring for file changes. It seems to have been supported in the past, but was removed a few months ago. Perhaps it will be introduced again in a future patch.

From: https://strongloop.com/strongblog/comparison-tools-to-automate-restarting-node-js-server-after-code-changes-forever-nodemon-nodesupervisor-nodedev/?utm_source=buffer&utm_campaign=Buffer&utm_content=buffercc5eb&utm_medium=appdotnet
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值