xenoterracide
xenoterracide

Reputation: 16847

Why can't I require(...) in a loop using browserify?

This works

require( './AppCtrl' );

but if I do

[ './AppCtrl' ].forEach( function( name ) {
    require( name );
});

it results in errors

_prelude.js:1 Uncaught Error: Cannot find module './AppCtrl's @  _prelude.js:1s @ _prelude.js:1(anonymous function) @ _prelude.js:1(anonymous function) @ index.js:48 @ index.js:3s @ _prelude.js:1(anonymous function) @ _prelude.js:11../config @ app.js:22s @ _prelude.js:1e @ _prelude.js:1(anonymous function) @ _prelude.js:1
 angular.js:12416 Error: [ng:areq] Argument 'AppCtrl' is not a function, got undefined
 http://errors.angularjs.org/1.4.5/ng/areq?p0=AppCtrl&p1=not%20a%20function%2C%20got%20undefined
at REGEX_STRING_REGEXP (angular.js:68)
at assertArg (angular.js:1795)
at assertArgFn (angular.js:1805)
at angular.js:9069
at setupControllers (angular.js:8133)
at nodeLinkFn (angular.js:8173)
at compositeLinkFn (angular.js:7637)
at publicLinkFn (angular.js:7512)
at angular.js:1660
at Scope.parent.$get.Scope.$eval (angular.js:15878)

for my end goal I'd like to be able to do something like this pseudocode

 foreach  name {
     angular.module(...).controller( require( name ) );
 }

but right now I'm not sure why I can't require files inside the loop?

Upvotes: 5

Views: 722

Answers (1)

JMM
JMM

Reputation: 26807

Because Browserify (via Detective) builds the dependency graph based on static analysis and therefore will only process require() calls with string literal arguments.

Upvotes: 5

Related Questions