2

I would like to be able to configure at the run time which modules to load in browser like that:

var moduleName  =  'my-module'
var module = require(moduleName)

It does't seem to work out of the box with but perhaps there is some way?

Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110

1 Answers1

0

Yes, There are many ways

  1. Webpack Hot Module Replacement Feature

    https://webpack.github.io/docs/hot-module-replacement.html

Example from Webpack Docs

var requestHandler = require("./handler.js");
var server = require("http").createServer();
server.on("request", requestHandler);
server.listen(8080);

// check if HMR is enabled
if(module.hot) {
    // accept update of dependency
    module.hot.accept("./handler.js", function() {
        // replace request handler of server
        server.removeListener("request", requestHandler);
        requestHandler = require("./handler.js");
        server.on("request", requestHandler);
    });
}

For methods check this answer

How do I include a JavaScript file in another JavaScript file?

Community
  • 1
  • 1
Navneet Singh
  • 1,218
  • 11
  • 17
  • Thank you, but will it only work with webpack running? Can I use it to compile production bundle that would load the module? – Dmitri Zaitsev Apr 30 '17 at 02:08