简体   繁体   English

Node.JS 中的热交换。 可能吗?

[英]Hot swapping in Node.JS. Possible?

Is it possible to implement or at least simulate code hot swapping in Node.JS?是否可以在 Node.JS 中实现或至少模拟代码热交换 If yes, how?如果是,如何?

Hot swapping (frequently called hot plugging) is replacing or adding components without stopping or shutting down the system.热插拔(通常称为热插拔)是在不停止或关闭系统的情况下更换或添加组件。 Erlang and Lisp support hot swapping natively. Erlang 和 Lisp 原生支持热插拔。

For commonjs modules (original node.js module system) you can hot-swap modules by deleting its cache and re-requiring them:对于 commonjs 模块(原始 node.js 模块系统),您可以通过删除其缓存并重新要求它们来热交换模块:

delete require.cache[require.resolve('my-module')];
require('my-module');

I'm not sure if this works with es6 modules.我不确定这是否适用于 es6 模块。

Of course, this needs to be done everywhere the module is loaded because otherwise your code will be using the objects and functions returned by the previous version of the module that is still in RAM.当然,这需要在加载模块的任何地方进行,否则您的代码将使用仍位于 RAM 中的先前版本模块返回的对象和函数。

One way to trigger a cascading reload is to make your main code also a module that is executed by a simple script that requires it.触发级联重新加载的一种方法是使您的主代码也是一个由需要它的简单脚本执行的模块。 Then reloading your main module would cause it to reload other modules that it uses.然后重新加载你的主模块会导致它重新加载它使用的其他模块。

You may not need hot-swapping您可能不需要热插拔

In actual practice however you may find that you don't really need hot-swapping.但是在实际实践中,您可能会发现您并不真正需要热插拔。 Most node.js servers take milliseconds to boot.大多数 node.js 服务器需要几毫秒才能启动。 Part of the reason for this is most I/O libraries in node.js connect to external services lazily.部分原因是 node.js 中的大多数 I/O 库都懒惰地连接到外部服务。 Node.js servers generally don't wait for database connection to succeed before executing the rest of the code. Node.js 服务器通常不会在执行其余代码之前等待数据库连接成功。 Instead it will try to connect to the database the first time you make a database request.相反,它会在您第一次发出数据库请求时尝试连接到数据库。

Also, javascript is a fast language to parse and compile (by necessity because you send the source to web pages)此外,javascript 是一种解析和编译的快速语言(这是必需的,因为您将源代码发送到网页)

Old trick with Webpack https://github.com/minimal-xyz/minimal-webpack-nodejs-hmr . Webpack https://github.com/minimal-xyz/minimal-webpack-nodejs-hmr 的老技巧。

Webpack has support for hot module replacement and Webpack also supports compiling Node.js apps, and it works to combined these two features. Webpack 支持热模块替换,Webpack 还支持编译 Node.js 应用程序,它可以将这两个特性结合起来。 Notice: it's designed for development, NOT for deployment.注意:它是为开发而设计的,而不是为部署而设计的。

While it's already a viable solution, I consider Webpack includes too many features which makes it quite heavy.虽然它已经是一个可行的解决方案,但我认为 Webpack 包含太多的功能,这使得它非常沉重。 I'm still in hope that we have lighter solution for that one day like how it's supporting HMR by default in ClojureScript.我仍然希望有一天我们有更轻松的解决方案,比如它如何在 ClojureScript 中默认支持 HMR。

Yes, you can use eval to hot-swap code in Node.是的,您可以使用evaleval中热交换代码。

let fn = () => console.log('foo');
fn(); // foo
eval(`fn = () => console.log('bar');`); // hot swap the source of fn
fn(); // bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM