user3762146
user3762146

Reputation: 215

How to call a function of a module dynamically in node js?

I want to do something like this.

var abc = require('./index')
var x = "someModule";
abc.x.someFunction();

So basically I want to do something like this:

abc.someModule.someFunction();

How do I achieve this?

Upvotes: 0

Views: 555

Answers (2)

Muhammad Usman
Muhammad Usman

Reputation: 10148

Use bracket notation

Something like

var x = "someModule";
abc[x].someFunction();

Upvotes: 1

Sello Mkantjwa
Sello Mkantjwa

Reputation: 1915

This should work:

var abc = require('./index')
var x = "someModule";
abc.[x].someFunction();

Upvotes: 0

Related Questions