Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
745 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...