I created a simple JavaScript application using AngularJS .
I use npm and Bower to manage my Gulp dependencies to automate my tasks, and I want to use CommonJS ' module.exports / require() to bundle everything together: I decided to go to Browserify to bundle it all.
There, my very empty and clean project on Github , if you want to take a look.
To be able to require('angular') , I configured Browserify to fine tune AngularJS into available modules using browserify-shim . Pretty simple, here is the relevant part of my package.json :
"browser": { "angular": "./bower_components/angular/angular.min.js" }, "browserify": { "transform": [ "browserify-shim" ] }, "browserify-shim": { "angular": { "exports": "angular" } }
This is pretty neat, my main JS file now looks like this:
'use strict'; var angular = require('angular'); angular.module('MyApp', []) .controller('View1Ctrl', ['$scope', require('./view1/view1.js')]) .controller('View2Ctrl', ['$scope', require('./view2/view2.js')]);
And it works.
Now I'm trying to move on to more advanced materials using external libraries available through Bower . They are installed under bower_components and look the same as my project, they have package.json , bower.json and thatβs it.
Take ng-dialog , for example, which also require('angular') . Having received through bower install ng-dialog --save , I do two things:
- Link your JS file with their answer with the keyword (say
ng-dialog ) in my package.json require('ng-dialog') is basically the main JS file so that my Angular module depends on them.
Here's the updated corresponding part of my package.json (note that ng-dialog is not required):
"browser": { "angular": "./bower_components/angular/angular.min.js", "ng-dialog": "./bower_components/ng-dialog/js/ngDialog.min.js" }, "browserify": { "transform": [ "browserify-shim" ] }, "browserify-shim": { "angular": { "exports": "angular" } }
... and the updated app.js file:
'use strict'; var angular = require('angular'); require('ng-dialog'); angular.module('MyApp', ['ngDialog']) .controller('View1Ctrl', ['$scope', require('./view1/view1.js')]) .controller('View2Ctrl', ['$scope', require('./view2/view2.js')]);
I get the following error while the Browser is :
Error: Cannot find module 'angular' from 'C:\...\bower_components\ng-dialog\js'
After a good half hour of customization, it turned out that SIMPLY deletes the package.json file from bower_components/ng-dialog makes everything go smoothly.
What happens and how the hell should I combine this ngDialog.min.js ?