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

javascript - How to reload a required module at runtime? - Stack Overflow

programmeradmin1浏览0评论

I would like to know how to reload a 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 multiple ways to reload my module required in the index.js module. But the Express app won't restart.

I would like to keep the index.js running, because it handles recompiling Babel modules on the fly. And the app.js module with the express server needs to be completely restarted.

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

I would like to know how to reload a 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 multiple ways to reload my module required in the index.js module. But the Express app won't restart.

I would like to keep the index.js running, because it handles recompiling Babel modules on the fly. And the app.js module with the express server needs to be completely restarted.

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

Share Improve this question edited Dec 6, 2016 at 16:48 Aurora0001 13.5k5 gold badges51 silver badges53 bronze badges asked Dec 3, 2016 at 10:02 Playdome.ioPlaydome.io 3,2252 gold badges20 silver badges35 bronze badges 1
  • Try browser-sync by npm install -g browser-sync – Venkat.R Commented Dec 5, 2016 at 18:06
Add a comment  | 

4 Answers 4

Reset to default 11 +50

require-uncached is an npm module that will clear the cache and load a module from source fresh each time, which is exactly what you seem to be interested in according to your title. To use this, you can simply use the following code:

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

Before doing this, it's probably necessary to ensure that all of the previous app.js's code (including the Express server) is stopped so that they don't conflict for the same port.

Also, I would suggest considering whether this approach is really the best answer - I'm sure a library such as pm2 could handle stopping and restarting a Node instance without the risk of unwanted data hanging around, which might cause a memory leak.

It's simple now, I prefer write it myself for less dependencies .


function clearModule(moduleName) {
  let mp = require.resolve(moduleName)
  if (require.cache[mp]) {
    delete require.cache[mp]
    console.log(`[clear] module: ${mp}`)
  }
}

function requireReload(moduleName) {
  clearModule(moduleName);
  return require(moduleName);
}

If all you're looking to do is restart the server without restarting the process I would recommend using the http.Server.close method. According to the express docs the app.listen method returns an http.Server object, so a reload would 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"

I would suggest you to use lazyload in web pack where I have my own post in this link.

First App

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 App

(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 a live Plunker

发布评论

评论列表(0)

  1. 暂无评论