How to reload the required module at runtime?

I would like to know how to reload the Node.js module at runtime.

Let's say I have the following code:

index.js

var myModule = require("./app.js"); 

app.js

 var express = require("express"); express.listen(3000, function() { console.log("app listening on port 3000"); }); 

I tried several ways to reload my module needed in the index.js module. But the Express application will not restart.

I want index.js to work, because it handles recompiling Babel modules on the fly. And the app.js module with the express server should be completely restarted.

Is there a way to do this without starting the second process for app.js?

+6
source share
3 answers

require-uncached is an npm module that clears the cache and loads the module from the source each time, which seems to be interesting to you according to your name. To use this, you can simply use the following code:

 const requireUncached = require('require-uncached'); requireUncached('./app.js'); 

Before doing this, you probably need to make sure that all the previous app.js code (including the Express server) is stopped so that they do not conflict for the same port.

In addition, I would suggest considering whether this approach is really the best answer - I am sure that a library such as pm2 can handle stopping and restarting a Node instance without risking damaging unwanted data, which could cause a memory leak.

+5
source

If all you want to do is restart the server without restarting the process, I would recommend using the http.Server.close method. According to the docs expression, the app.listen method returns an http.Server object, so the reboot will look something like this:

app.js

 const express = require("express"); app = express(); // Define the listener function separately so it can be used on restart function listener() { console.log("app listening on port 3000"); } // start the server and save a reference to it for our restart function const server = app.listen(3000, listener); // Export a restart function to be used by index.js exports.restart = function() { server.close(function() { // Server is closed, start listening again server.listen(3000, listener) // Use the same listener function as app.listen }); }; 

index.js

 const myModule = require("./app.js"); // Output: "app listening on port 3000" // Restart the server, keep the process myModule.restart(); // Output: "app listening on port 3000" 
+2
source

I suggest you use lazyload in a web package where I have my own post in this link .

First application

 angular.module('myApp', ['ui.router','oc.lazyLoad']) .config(function ($stateProvider, $locationProvider, $ocLazyLoadProvider) { $stateProvider .state("home", { url: "/home", templateUrl: "Home.html", controller: 'homeCtrl', resolve: { loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) { return $ocLazyLoad.load('homeCtrl.js'); }] } }) .state("profile", { url:"/profile", templateUrl: "profile.html", resolve: { loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) { return $ocLazyLoad.load('someModule.js'); }] } }) }); 

Second application

 (function () { var mynewapp=angular.module('someApp',['myApp']); mynewapp.config(function(){ //your code to route from here! }); mynewapp.controller("profileCtrl", function ($scope) { console.log("reached profile controller"); }); })(); 

also lively plunker

+2
source

Source: https://habr.com/ru/post/1012858/


All Articles