I'm trying to setup my own nodejs server, but I'm having a problem. I can't figure out how to see changes to my application without restarting it. Is there a way to edit the application and see changes live with node.js?
我正在尝试设置我自己的nodejs服务器,但是我遇到了一个问题。如果不重新启动应用程序,我就无法看到对应用程序的更改。是否有一种方法可以使用node.js编辑应用程序并查看更改?
9 个解决方案
#1
47
Check out Node-Supervisor. You can give it a collection of files to watch for changes, and it restarts your server if any of them change. It also restarts it if it crashes for some other reason.
查看Node-Supervisor。您可以给它一个文件集合来监视更改,如果有任何更改,它将重新启动您的服务器。如果它因为其他原因崩溃,它也会重新启动。
"Hot-swapping" code is not enabled in NodeJS because it is so easy to accidentally end up with memory leaks or multiple copies of objects that aren't being garbage collected. Node is about making your programs accidentally fast, not accidentally leaky.
NodeJS中不支持“热交换”代码,因为很容易意外地导致内存泄漏或没有被垃圾收集的对象的多个副本。Node是关于使你的程序意外地快速,而不是偶然地泄漏。
EDIT, 7 years after the fact: Disclaimer, I wrote node-supervisor, but had handed the project off to another maintainer before writing this answer.
编辑,7年后的事实:免责声明,我写了node-supervisor,但是在写这个答案之前已经把项目交给了另一个维护者。
#2
58
Nodules is a module loader for Node that handles auto-reloading of modules without restarting the server (since that is what you were asking about):
Nodules是一个用于节点的模块加载程序,它可以在不重新启动服务器的情况下自动重新加载模块(因为这正是您要问的):
http://github.com/kriszyp/nodules
http://github.com/kriszyp/nodules
Nodules does intelligent dependency tracking so the appropriate module factories are re-executed to preserve correct references when modules are reloaded without requiring a full restart.
Nodules会执行智能依赖跟踪,因此在不需要重新启动模块的情况下,将重新执行相应的模块工厂以保留正确的引用。
#3
9
Use this: https://github.com/remy/nodemon
使用:https://github.com/remy/nodemon
Just run your app like this: nodemon yourApp.js
就像这样运行你的应用:nodemon your app .js
#4
3
What's “Live Coding”?
“实时编码”是什么?
In essence, it's a way to alter the program while it runs, without restarting it. The goal, however, is to end up with a program that works properly when we (re)start it. To be useful, it helps to have an editor that can be customized to send code to the server.
本质上,它是一种在程序运行时改变程序而不重新启动的方法。然而,我们的目标是在我们重新启动程序时使用一个正常的程序。有用的是,有一个编辑器可以定制为向服务器发送代码。
Take a look: http://lisperator.net/blog/livenode-live-code-your-nodejs-application/
看一看:http://lisperator.net/blog/livenode-live-code-your-nodejs-application/
#5
3
There should be some emphasis on what's happening, instead of just shotgunning modules at the OP. Also, we don't know that the files he is editing are all JS modules or that they are all using the "require" call. Take the following scenarios with a grain of salt, they are only meant to describe what is happening so you know how to work with it.
这里应该强调正在发生的事情,而不是在操作平台上随意射击模块。此外,我们不知道他正在编辑的文件都是JS模块,也不知道它们都在使用“require”调用。以下面的场景为例,它们只是用来描述正在发生的事情,这样你就知道如何使用它了。
-
Your code has already been loaded and the server is running with it
您的代码已经被加载,服务器正在使用它运行
-
SOLUTION You need to have a way to tell the server what code has changed so that it can reload it. You could have an endpoint set up to receive a signal, a command on the command line or a request through tcp/http that will tell it what file changed and the endpoint will reload it.
解决方案您需要有一种方法来告诉服务器什么代码已经更改,以便它可以重新加载它。您可以设置一个端点来接收一个信号,命令行上的一个命令,或者通过tcp/http的一个请求,该请求将告诉它修改了什么文件,端点将重新加载它。
//using Express var fs = require('fs'); app.get('reload/:file', function (req, res) { fs.readfile(req.params.file, function (err, buffer) { //do stuff... }); });
-
-
Your code may have "require" calls in it which loads and caches modules
您的代码中可能有“需要”调用,用于加载和缓存模块
-
SOLUTION since these modules are cached by require, following the previous solution, you would need a line in your endpoint to delete that reference
由于这些模块是由require缓存的,按照前面的解决方案,您将需要端点中的一行来删除该引用
var moduleName = req.params.file; delete require.cache[moduleName]; require('./' + moduleName);
-
There's a lot of caveats to get into behind all of this, but hopefully you have a better idea of what's happening and why.
在这一切背后有很多需要注意的地方,但希望你能对发生的事情和原因有一个更好的了解。
#6
3
if you would like to reload a module without restarting the node process, you can do this by the help of the watchFile function in fs module and cache clearing feature of require:
如果你想在不重新启动节点进程的情况下重新加载一个模块,你可以借助fs模块中的watchFile函数和require的缓存清理功能:
Lets say you loaded a module with a simple require:
让我们假设您加载了一个具有简单需求的模块:
var my_module = require('./my_module');
In order to watch that file and reload when updated add the following to a convenient place in your code.
为了查看该文件并在更新时重新加载,在代码中添加以下内容。
fs.watchFile(require.resolve('./my_module'), function () {
console.log("Module changed, reloading...");
delete require.cache[require.resolve('./my_module')]
my_module = require('./my_module');
});
If your module is required in multiple files this operation will not affect other assignments, so keeping module in a global variable and using it where it is needed from global rather than requiring several times is an option. So the code above will be like this:
如果您的模块在多个文件中都是必需的,那么此操作将不会影响其他分配,因此将模块保存在全局变量中,并在需要的地方使用它,而不是多次使用它,这是一个选项。上面的代码是这样的:
global.my_module = require ('./my_module');
//..
fs.watchFile(require.resolve('./my_module'), function () {
console.log("Module changed, reloading...");
delete require.cache[require.resolve('./my_module')]
global.my_module = require('./my_module');
});
#7
1
I think node-inspector is your best bet.
我认为点探员是你最好的选择。
Similar to how you can Live Edit Client side JS code in Chrome Dev tools, this utilizes the Chrome (Blink) Dev Tools Interface to provide live code editing.
类似于在Chrome开发工具中如何实时编辑客户端JS代码,它利用Chrome (Blink)开发工具界面提供实时代码编辑。
https://github.com/node-inspector/node-inspector/wiki/LiveEdit
https://github.com/node-inspector/node-inspector/wiki/LiveEdit
#8
1
You can also use the tool PM2. Which is a advanced production process tool for node js. http://pm2.keymetrics.io/
您还可以使用工具PM2。这是node js的一个高级生产过程工具。http://pm2.keymetrics.io/
#9
0
A simple direct solution with reference to all answers available here:
一个简单的直接解决方案,参考这里所有的答案:
Node documentation says that fs.watch is more efficient than fs.watchFile & it can watch an entire folder.
节点文档说fs。手表比fs更有效率。watchFile &它可以监视整个文件夹。
(I just started using this, so not really sure whether there are any drawbacks)
(我刚开始用这个,所以不确定是否有什么缺点)
fs.watch("lib", (event_type, file_name) => {
console.log("Deleting Require cache for " + file_name);
delete require.cache[ require.resolve("./lib/" + file_name)];
});
#1
47
Check out Node-Supervisor. You can give it a collection of files to watch for changes, and it restarts your server if any of them change. It also restarts it if it crashes for some other reason.
查看Node-Supervisor。您可以给它一个文件集合来监视更改,如果有任何更改,它将重新启动您的服务器。如果它因为其他原因崩溃,它也会重新启动。
"Hot-swapping" code is not enabled in NodeJS because it is so easy to accidentally end up with memory leaks or multiple copies of objects that aren't being garbage collected. Node is about making your programs accidentally fast, not accidentally leaky.
NodeJS中不支持“热交换”代码,因为很容易意外地导致内存泄漏或没有被垃圾收集的对象的多个副本。Node是关于使你的程序意外地快速,而不是偶然地泄漏。
EDIT, 7 years after the fact: Disclaimer, I wrote node-supervisor, but had handed the project off to another maintainer before writing this answer.
编辑,7年后的事实:免责声明,我写了node-supervisor,但是在写这个答案之前已经把项目交给了另一个维护者。
#2
58
Nodules is a module loader for Node that handles auto-reloading of modules without restarting the server (since that is what you were asking about):
Nodules是一个用于节点的模块加载程序,它可以在不重新启动服务器的情况下自动重新加载模块(因为这正是您要问的):
http://github.com/kriszyp/nodules
http://github.com/kriszyp/nodules
Nodules does intelligent dependency tracking so the appropriate module factories are re-executed to preserve correct references when modules are reloaded without requiring a full restart.
Nodules会执行智能依赖跟踪,因此在不需要重新启动模块的情况下,将重新执行相应的模块工厂以保留正确的引用。
#3
9
Use this: https://github.com/remy/nodemon
使用:https://github.com/remy/nodemon
Just run your app like this: nodemon yourApp.js
就像这样运行你的应用:nodemon your app .js
#4
3
What's “Live Coding”?
“实时编码”是什么?
In essence, it's a way to alter the program while it runs, without restarting it. The goal, however, is to end up with a program that works properly when we (re)start it. To be useful, it helps to have an editor that can be customized to send code to the server.
本质上,它是一种在程序运行时改变程序而不重新启动的方法。然而,我们的目标是在我们重新启动程序时使用一个正常的程序。有用的是,有一个编辑器可以定制为向服务器发送代码。
Take a look: http://lisperator.net/blog/livenode-live-code-your-nodejs-application/
看一看:http://lisperator.net/blog/livenode-live-code-your-nodejs-application/
#5
3
There should be some emphasis on what's happening, instead of just shotgunning modules at the OP. Also, we don't know that the files he is editing are all JS modules or that they are all using the "require" call. Take the following scenarios with a grain of salt, they are only meant to describe what is happening so you know how to work with it.
这里应该强调正在发生的事情,而不是在操作平台上随意射击模块。此外,我们不知道他正在编辑的文件都是JS模块,也不知道它们都在使用“require”调用。以下面的场景为例,它们只是用来描述正在发生的事情,这样你就知道如何使用它了。
-
Your code has already been loaded and the server is running with it
您的代码已经被加载,服务器正在使用它运行
-
SOLUTION You need to have a way to tell the server what code has changed so that it can reload it. You could have an endpoint set up to receive a signal, a command on the command line or a request through tcp/http that will tell it what file changed and the endpoint will reload it.
解决方案您需要有一种方法来告诉服务器什么代码已经更改,以便它可以重新加载它。您可以设置一个端点来接收一个信号,命令行上的一个命令,或者通过tcp/http的一个请求,该请求将告诉它修改了什么文件,端点将重新加载它。
//using Express var fs = require('fs'); app.get('reload/:file', function (req, res) { fs.readfile(req.params.file, function (err, buffer) { //do stuff... }); });
-
-
Your code may have "require" calls in it which loads and caches modules
您的代码中可能有“需要”调用,用于加载和缓存模块
-
SOLUTION since these modules are cached by require, following the previous solution, you would need a line in your endpoint to delete that reference
由于这些模块是由require缓存的,按照前面的解决方案,您将需要端点中的一行来删除该引用
var moduleName = req.params.file; delete require.cache[moduleName]; require('./' + moduleName);
-
There's a lot of caveats to get into behind all of this, but hopefully you have a better idea of what's happening and why.
在这一切背后有很多需要注意的地方,但希望你能对发生的事情和原因有一个更好的了解。
#6
3
if you would like to reload a module without restarting the node process, you can do this by the help of the watchFile function in fs module and cache clearing feature of require:
如果你想在不重新启动节点进程的情况下重新加载一个模块,你可以借助fs模块中的watchFile函数和require的缓存清理功能:
Lets say you loaded a module with a simple require:
让我们假设您加载了一个具有简单需求的模块:
var my_module = require('./my_module');
In order to watch that file and reload when updated add the following to a convenient place in your code.
为了查看该文件并在更新时重新加载,在代码中添加以下内容。
fs.watchFile(require.resolve('./my_module'), function () {
console.log("Module changed, reloading...");
delete require.cache[require.resolve('./my_module')]
my_module = require('./my_module');
});
If your module is required in multiple files this operation will not affect other assignments, so keeping module in a global variable and using it where it is needed from global rather than requiring several times is an option. So the code above will be like this:
如果您的模块在多个文件中都是必需的,那么此操作将不会影响其他分配,因此将模块保存在全局变量中,并在需要的地方使用它,而不是多次使用它,这是一个选项。上面的代码是这样的:
global.my_module = require ('./my_module');
//..
fs.watchFile(require.resolve('./my_module'), function () {
console.log("Module changed, reloading...");
delete require.cache[require.resolve('./my_module')]
global.my_module = require('./my_module');
});
#7
1
I think node-inspector is your best bet.
我认为点探员是你最好的选择。
Similar to how you can Live Edit Client side JS code in Chrome Dev tools, this utilizes the Chrome (Blink) Dev Tools Interface to provide live code editing.
类似于在Chrome开发工具中如何实时编辑客户端JS代码,它利用Chrome (Blink)开发工具界面提供实时代码编辑。
https://github.com/node-inspector/node-inspector/wiki/LiveEdit
https://github.com/node-inspector/node-inspector/wiki/LiveEdit
#8
1
You can also use the tool PM2. Which is a advanced production process tool for node js. http://pm2.keymetrics.io/
您还可以使用工具PM2。这是node js的一个高级生产过程工具。http://pm2.keymetrics.io/
#9
0
A simple direct solution with reference to all answers available here:
一个简单的直接解决方案,参考这里所有的答案:
Node documentation says that fs.watch is more efficient than fs.watchFile & it can watch an entire folder.
节点文档说fs。手表比fs更有效率。watchFile &它可以监视整个文件夹。
(I just started using this, so not really sure whether there are any drawbacks)
(我刚开始用这个,所以不确定是否有什么缺点)
fs.watch("lib", (event_type, file_name) => {
console.log("Deleting Require cache for " + file_name);
delete require.cache[ require.resolve("./lib/" + file_name)];
});