Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I require a constructor and immediately use it with browserify?

The two following lines of code should do exactly the same thing. The first one is a bit more verbose, but this should be the only difference. Still, the 2nd example results in an error. Why?

The following works:

var Model = require('./Model');
new Model();

However, the following results in Uncaught Error: Cannot find module './Model'

new require('./Model')();
like image 955
ragulka Avatar asked Dec 07 '25 02:12

ragulka


1 Answers

It has to do with the operator precedence. If you do this, it will work:

new (require("./Model"))();

What was happening is:

(new require("./Model"))()
like image 81
cookie monster Avatar answered Dec 08 '25 14:12

cookie monster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!