We have a Node.js widget that gets exported like so:
module.exports = ourWidget;
I then import it into our server.js like so:
var ourWidget = require('./ourWidget');
var ow = new ourWidget;
This works as expected, but could it be done in a single line? EG:
var ow = new (require('./ourWidget'));
Which doesn't work, I've also tried this:
var ow = new (require('./ourWidget')());
Both of which resembles the code in this SO question: How does require work with new operator in node.js?, but both fail as soon as I try to run the code.
You can achieve this by shifting the function call to outside of the wrapping parens:
var ow = new (require('./ourWidget'))()
But keep in mind you now have no way of accessing the original widget constructor (this may or may not be a bad thing in your case).