Why can't I request (...) in a loop using a browser?

It works

require( './AppCtrl' ); 

but if i do

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

this leads to errors

 _prelude.js:1 Uncaught Error: Cannot find module './AppCtrl @ _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 ultimate goal I would like to do something like this pseudocode

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

but now i'm not sure why i cant require files inside the loop?

+5
source share
1 answer

Since Browserify (via Detective) builds a dependency graph based on static analysis and therefore handles require() calls with string literal arguments.

+5
source

All Articles