happy break
happy break

Reputation: 99

webpack can not require variable ,the request of a dependency is an expression

how to require a variable. i can work like this

var config={

    example1 : require("example1/main.js"),

    example2 : require("example2/main.js")

}

when I use like

var modules=["example1","example2"]

_.each(modules,function(module){

    require(module+"/main.js")

})

this error is "Cannot find module 'example1/main.js"

Upvotes: 9

Views: 17028

Answers (1)

JuanjoFR
JuanjoFR

Reputation: 471

I had a similar problem and solved it by specifying the full path, e.g., in your case:

var modules=["example1","example2"];

_.each(modules,function(module){

  require("./"+module+"/main.js");

});

Maybe this example can help you.

This type of error is related to the context.

Upvotes: 14

Related Questions