Start Use Gulp and Browserify to Build Angular App with Node.js

2016/4/14 min read
bookmark this
Responsive image

Use Gulp, Gulp Plugins and Browserify to build Angular app

This sample app use following technologies.

  1. Node.js
  2. Angular.js
  3. Browserify
  4. Gulp
  5. gulp-connect
  6. gulp-ng-annotate

The files structure will be following, also you can check the sample code at Github. After you download the code, run following command should be able to start your app. The fisrt command will install all the node.js dependecy modules, and second command will run the gulp task defined at gulpfile.js

npm install -d

App folder are all the client side file for build angular app.

Gulpfile contains all the client side task build.

Index.html is the html page will be request by the client, also contains the angular app.

package.json contains all the dependencies for this example.

  • gulp-test1
    • app
      • controllers
        • controller1.js
        • controller2.js
        • controller3.js
      • views
        • controller1.html
        • controller2.html
        • controller3.html
      • app.js
    • gulpfile.js
    • index.html
    • package.json


The main module here are angular, angular-route and browserify, because browserify will take care of angular app's dependency that's why need angular as node module. Also, gulp and gulp's plugin modules, the gulp-np-annotate will fix the issue when mininize js in angular, gulp-connect will run a test app.

  "name": "gulp-test1",
  "version": "1.0.0",
  "description": "gulp-test1",
  "dependencies": {
    "angular": "^1.5.5",
    "gulp": "^3.9.1"
  "devDependencies": {
    "angular": "^1.5.5",
    "angular-route": "^1.5.5",
    "browserify": "^13.0.0",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.0",
    "gulp-connect": "^3.2.2",
    "gulp-cssmin": "^0.1.7",
    "gulp-ng-annotate": "^2.0.0",
    "gulp-uglify": "^1.5.3",
    "vinyl-source-stream": "^1.1.0"
  "author": "dake"

Angular App

Following is the example code use browserify with angular, broserify will take all the dependencies for angular. For example, following code is the app.js at angular.js, which define where is the controller and views, in angualr it is useing angular-route, and ofcourse, app.js is also depend on angular.js. In Browserify, it is very simple to define, you just need to write, require('angular'), browserify will take care the dependency for you. Similar to the angular controllers, you'll need to define as require('./controllers/controller1');.

'use strict';
var app = angular.module('gulp.test1', ['ngRoute']);


app.config(function ($routeProvider) {
       .when('/', {
        templateUrl: 'views/controller1.html',
        controller: 'controller1',
        controllerAs: 'controller1'
      .when('/1', {
        templateUrl: 'views/controller2.html',
        controller: 'controller2',
        controllerAs: 'controller2'
       .when('/2', {
        templateUrl: 'views/controller3.html',
        controller: 'controller3',
        controllerAs: 'controller3'
        redirectTo: '/'

'use strict';

  .controller('controller1', function ($scope) {
    $scope.message = 'controller1!';
<h2>message: {{message}}</h2>

How do I use browserify to bundle all javascript

This is sample task at gulpfile.js, at end of this task, it will create a javascript combined all the dependency javascript. In our previous app.js. It shouldn't contains angular.js angular-route.js, all the angular controllers and the app.js itself.

gulp.task('browserify', function () {
    return browserify('./app/app.js')


In this example, index.html is the actual web page, as you can see it is any reference to the minified javascript.

<!doctype html>
    <meta charset="utf-8">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <script src="/js/app.min.js"></script>
    <div  ng-app="gulp.test1">
      <div ng-view></div>
      <li><a href="/#">home</a></li>
      <li><a href="/#1">main</a></li>
      <li><a href="/#2">main1</a></li>


/// setup gulp 

var gulp = require('gulp');
var connect = require('gulp-connect');
// requires browserify and vinyl-source-stream
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var concat = require('gulp-concat');
//var cssmin = require("gulp-cssmin");
var uglify = require("gulp-uglify");
var ngAnnotate = require('gulp-ng-annotate');

gulp.task('connect', function () {
        root: 'public',
        port: 4000

gulp.task('browserify', function () {
    // Grabs the app.js file
    return browserify('./app/app.js')
        // bundles it and creates a file called main.js
        // saves it the public/js/ directory

gulp.task("min:js", function () {

    gulp.src(["./public/js/app.js"], { base: "." })

gulp.task("min", ["min:js"]);

// Views task
gulp.task('views', function () {

    // Any other view files from app/views
    // Will be put in the dist/views folder
    // copy html to production folder

gulp.task('watch', function () {'app/**/*.js', ['browserify','views']);

gulp.task('default', ['browserify', 'views', 'min', 'watch','connect']);

References or Links

  1. gulp's plugins There's thousands of plugins for gulp
  2. browserify Official website
  3. angular1 and angular2
  4. Most popular gulp plugins


Gulp is very powerful client side task runner, there're many different plugins you can use, also popular IDE visual studio is also start to use gulp/grunt as client side task runner to build assets. That means more people will start using it and create modules for it. Also with browserify it will even become more powerful, it use node.js to combined all client side resources.