javascriptwebpackmodulewebpack-3

How can I require arbitrary local script within a webpack app during runtime?


I'm trying to load some arbitrary modules during runtime which are not available during build time. Think of plugins which are only required at runtime. So something like:

require(plugin_dir + '/plugins.js')

needs to be loaded but webpack complains rightfully that this module is not available. How can I make this work?

I tried SystemJS to trick webpack but it figured out what I'm trying to accomplish and fails.


Solution

  • In the end I used https://github.com/ezze/node-require-wrapper

    It works like that:

    Webpack config file:

    module.exports = {
      // ...
      module: {
          rules: {
              // ...
          },
          noParse: /require-wrapper/
      }
    }
    

    Then in your code you can use it like this:

    var nodeRequire = require('require-wrapper');
    var helloModulePath = path.resolve(__dirname, 'dynamic/hello.js');