5

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());

});

5
  • 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 Commented May 6, 2015 at 6:30
  • thanks for pointing me out. Im going to read this now. Commented May 6, 2015 at 12:42
  • 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. Commented May 6, 2015 at 13:53
  • @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. Commented Jan 15, 2016 at 1:18
  • 1
    @Chic Allright Done :) Commented Jan 16, 2016 at 2:05

1 Answer 1

3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, for the lost masses if they exist could you drop in an example. I'm having the same issue figuring browserify out, but I don't understand your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.