5
votes

Alright, I have no clue. I searched the internet for like 3 days now and couldn't find any example on how to use Browserify in combination with AngularJS and Gulp. Yes there are examples but they all show that simple 'hello world' app structure nobody will use in the end anyway. They all write their little controllers inside the main app.js file. No modular setup. And the modular setups I found, well .. they include all the files by hand in the index.html file.. sigh (sorry for my tone).

What i try to achieve is to autoload all my application files but it just doesn't work. What do i need to do to include my controller files? Isn't that what browserify is for? Why isn't it working?

The first answer will probably be: you need to require() the files. But how? I tried require('myApp.mainController'); as well as require('src/features/main/main-controller.js') with the result of:

Error: No Module found.

If someone can point me in the right direction, please help me! :). Thanks in advance! The necessary info is below the line.


Project structure

|project
|-builds
| |-development
| |-production
|-src
| |-components
| |-features
| | |-main
| | | |-main-ctrl.js
| | | |-main.html
| | |-dashboard
| | | |-dashboard-ctrl.js
| | | |-dashboard.html
| |-app.js
| |-app.scss
| |-index.html

app.js

require('angular');
require('angular-ui-router');

var app = angular.module('myApp', [
  'ui.router',
  'myApp.mainController'
]).config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise('/');

  $stateProvider
    .state('main', {
      url: '/',
      templateUrl: 'src/features/main/main.html',
      controller: 'mainController'
    })
    .state('dashboard', {
      url: '/dashboard',
      templateUrl: 'src/features/dashboard/dashboard.html',
      controller: 'dashboardController'
    })
}]);

main-ctrl.js

angular.module('myApp.mainController', [])

  .controller('mainController', ['$scope', 'Main', function($scope, Main) {
    $scope.message = Main.message;
}]);

gulpfile.js

gulp.task('js', function() {
  return browserify({
    entries: 'src/app.js',
    debug: true
  })
    .bundle()
    .pipe(gulp.dest('builds/development'))
    .pipe(connect.reload());

});

1
The first thing to understand is that you can't mix up names from browserify (CommonJS) modules with angular modules. They are not the same thing. You may end up with some CommonJS modules that have the same name as angular modules, but they're still different things. When you use browserify with angular, you end up with 3 layers of dependency management: CommonJS modules, angular's modules, and then angular's dependency injection. This article does a great job of explaining things step by step with a modular set up: blog.codecentric.de/en/2014/08/angularjs-browserify - user2943490
thanks for pointing me out. Im going to read this now. - Nique
Ok, for me its working now. I put an index.js file in every 'module' i create. This file is apparently seen as an module. From here i 'require' the controllers, factories/services and add them to my angular app. - Nique
@Nique You should post your solution as an answer to make it easy for others to find who may have had a similar problem. Then you can also accept it as the correct answer. - Chic
@Chic Allright Done :) - Nique

1 Answers

3
votes

This is how I resolved this problem.

  • I put an index.js file in every 'module' I created. This file is apparently seen as a module.
  • From here I require the controllers, factories/services and add them to my AngularJS app.