Building an AngularJS App with Yeoman
Scaffolding an AngularJS app with Yeoman, adding code with generators and libraries with Bower, then building it for production with Grunt.
This post was originally published in Japanese on Qiita.
Yeoman is a set of tools for front-end development. It covers the whole workflow: scaffolding a new project, managing libraries, running a local server, and building for production.
It’s made up of three tools:
- yo: scaffolds new projects
- Bower: manages front-end packages
- Grunt: runs tasks such as builds and the local server
Environment
I tested with these versions:
node --version
v0.10.24
npm --version
1.3.21
Installing Yeoman
You can install Node.js with Homebrew, which installs npm along with it. Then install yo with npm:
brew install node
npm install --global yo
This gives you the yo command. yo lists Bower and Grunt’s command-line tool (grunt-cli) as peer dependencies, so npm installs them too, giving you the bower and grunt commands.
Installing the AngularJS generator
Yeoman scaffolds projects with generators, one for each kind of project. There are official ones and plenty of third-party ones.
Install the one for AngularJS:
npm install --global generator-angular
Creating an AngularJS app
yo generates the project into the current directory and names the app after it, so start in a new, empty folder:
mkdir firstapp
cd firstapp
yo angular
_-----_
| |
|--(o)--| .--------------------------.
`---------´ | Welcome to Yeoman, |
( _´U`_ ) | ladies and gentlemen! |
/___A___\ '__________________________'
| ~ |
__'.___.'__
´ ` |° ´ Y `
Out of the box I include Bootstrap and some AngularJS recommended modules.
[?] Would you like to use Sass (with Compass)? No
[?] Would you like to include Twitter Bootstrap? Yes
[?] Which modules would you like to include?
yo asks what to include. I skipped Sass, which needs Ruby and the Compass gem, and took the plain CSS version of Bootstrap. Saying yes to Sass adds a third question about using the Sass version of Bootstrap instead.
It then generates the project files and runs npm install and bower install to fetch the dependencies.
Starting the Grunt server
The generated project comes with a Grunt task that starts a local development server:
grunt serve
After a moment, your browser opens and shows this page:

If you see this page, Yeoman and its tools are set up correctly.
Extending the app
As the app grows, you’ll want to add controllers and services. generator-angular has a subgenerator for each.
Let’s add a service:
yo angular:service myService
create app/scripts/services/myService.js
create test/spec/services/myService.js
This generates the service and its test, and adds a script tag to app/index.html:
<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
+ <script src="scripts/services/myService.js"></script>
<!-- endbuild -->
Adding a library with Bower
When you need a library, check Bower first. Let’s add Font Awesome:
bower search awesome
Search results:
font-awesome git://github.com/FortAwesome/Font-Awesome.git
components-font-awesome git://github.com/components/font-awesome.git
...
Install it with --save to record it as a dependency in bower.json. Then run grunt bower-install, which adds the files of your Bower packages to app/index.html between the <!-- bower:css --> and <!-- bower:js --> markers. grunt serve runs it too.
bower install --save font-awesome
grunt bower-install
font-awesome was not injected in your file.
Please go take a look in "app/bower_components/font-awesome" for the file you need, then manually include it in your file.
The task injects whatever each package lists as its main file in bower.json, but Font Awesome 4.0.3 doesn’t ship a bower.json at all.
The fix is the Components project. It republishes popular libraries packaged properly for each package manager, and its Bower packages are named components-*:
There are many packages managers… More than for each language! And for each one you need a mirror repository with built
*.js.Components is here to centralize this, having only one repository by project, supporting every package manager there is and keeping project repositories up-to-date.
components-font-awesome declares css/font-awesome.css as its main file. Swap it in and run the task again:
bower uninstall --save font-awesome
bower install --save components-font-awesome
grunt bower-install
Now the stylesheet is in app/index.html:
<!-- bower:css -->
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
+ <link rel="stylesheet" href="bower_components/components-font-awesome/css/font-awesome.css" />
<!-- endbower -->
Building and deploying
Generate files optimized for production:
grunt build
Copy the generated dist directory to your web server’s document root, and you’re deployed.