Try Tuts+ Premium, Get Cash Back!

Say Yo to Yeoman

Tutorial Details
  • Difficulty: Intermediate
  • Completion Time: 15 Minutes

According to yeoman.io, “Yeoman is a robust and opinionated set of tools, libraries, and a workflow that can help developers quickly build beautiful, compelling web apps.” Let’s dig in and see exactly what this means!


Yeoman

        _   .--------------------------.
      _|o|_ |    Welcome to Yeoman,    |
       |_|  |   ladies and gentlemen!  |
     / \Y/ \ o_________________________|
    ||  :  |//
    o/ --- \
      _\ /_
		

A great deal of work goes into building web apps these days. There’s countless libraries out there to use, patterns, styles, grids, boilerplates, bootstraps…the list goes on! Yeoman is an answer to some of these issues. Rather than having to waste a bunch of time getting an application up and running, Yeoman does the brunt of the work for you – only requiring a few commands.

Before we move forward, we have to install it!

The fastest way to begin using Yeoman is by running the following script.

				curl -L get.yeoman.io | sh
			
Note: Yeoman is supported by OSX, Linux, and Windows. This process clocks in around 10 minutes. In a future update, this installation process will be modified, however.

So, that was easy. What just happened? Lots of things! The install.sh file off ,get.yeoman.io, has a header that describes exactly what occurred.

# * Detect Mac or Linux, pick which package manager to use
# * On Mac, install homebrew if it's not present
# * Then install these: git optipng jpeg-turbo phantomjs
# * Make sure Ruby >= 1.8.7 is around, install if not (for Compass)
# * Install the latest NodeJS package
# * Install Compass
# * Download Yeoman zip to a temporary folder
# * Install it as a global node module

And there ya have it!

Use it

Yeoman is installed as a global Node module, so pop open your TOC (Terminal of Choice), and run yeoman.

The first time this runs, it will enquire if you want to allow them to keep stats on your Yeoman usage. They actually have a Google Analytics account setup to track all kinds of interesting usage statistics.

yeoman

Simply running yeoman from now on will print out a list of the commands that are available to execute.

yeoman init

This is where the scaffolding magic happens. Run the following command for a basic scaffolded app.

                yeoman init
            

This command will ask five questions:

yeoman init

Out of the box, running init will include HTML5 Boilerplate, and jQuery and Modernizr. Here’s a more expanded list.

  • HTML5 Boilerplate for the main base
  • Compass Twitter Bootstrap for the SASS files as the CSS files are authored in SASS
  • Twitter Bootstrap for the optional list of JavaScript plugins (optional)
  • RequireJS for AMD module and script loading support (optional)
  • RequireHM for experimental EcmaScript 6 module syntax support on top of RequireJS (optional)

It will also compile CoffeeScript and Compass files right out of the box!

Generators are the magic behind all of the scaffolding. The init command, itself, is based on a generator. There is a separate repository for them. There are many available already, including Backbone, Ember, and Angular to name just a few of them. To view a list of all them, simply run…

				yeoman init --help
			

Generators are a huge part of Yeoman, and are meant to be modified and added to.

Tip: For help on creating your own generator, refer to the generators area of the documentation.

If you want to create your own Backbone.js application, you could simply run:

				mkdir backboneapp && cd backboneapp
				yeoman init backbone
			

This will build a project with several boilerplate models, views, collections, preloaded with Lodash, Mocha, jQuery and HTML5 boilerplate.
You can do the same with angular and ember as mentioned previously.

There are various sub generators as well, which allow you to do things, like:

				yeoman init backbone:view awesomeView
				yeoman init backbone:model awesomeModel
				yeoman init backbone:collection awesomeCollection
				yeoman init backbone:router awesomeRouter
			

Then BAM, you’ll have some new codez to work with, rather than wasting a bunch of time rewriting boilerplate code!

yeoman build

Under the covers, you’ll find Grunt, which a popular framework, created by Ben Alman (Cowboy), for building JavaScript applications. It relies on a grunt file of configuration options that configure tasks to perform various operations, such as linting, combining, minifying, etc.

Yeoman is built on top of Grunt, but extends it to provide some new modifications and features. These include:

  • JSHint
  • Compiling CoffeeScript and SASS
  • Building RequireJS applications, using r.js
  • Concatenating, minifying, and compressing PNGs and JPEGS
  • Running tests with PhantomJS
  • Building an application cache manifest
  • And a few others.

The configuration will be placed within a gruntfile.js file in the generated app. When you’re finished developing, run the following to build the app.

				yeoman build
			

Your newly built app will be placed within a dist/ folder. One cool feature is how Yeoman will take script references, such as:

            <!-- build:js scripts/scripts.js -->
            <script src="scripts/vendor/jquery.min.js"></script>
            <script src="scripts/vendor/lodash.min.js"></script>
            <script src="scripts/vendor/backbone-min.js"></script>
            <script src="scripts/main.js"></script>
            <script src="scripts/views/application-view.js"></script>
            <script src="scripts/models/application-model.js"></script>
            <script src="scripts/collections/application-collection.js"></script>
            <script src="scripts/routes/app-router.js"></script>
            <script src="scripts/models/photo-model.js"></script>
            <script src="scripts/collections/photos-collection.js"></script>
            <script src="scripts/views/flickr-view.js"></script>
            <script src="scripts/views/photoItem-view.js"></script>
            <!-- endbuild -->
            

And, after running the build, concat and minify those files down to a single script reference. That’s twelve HTTP requests down to one!

            <script src="scripts/47ce0679.scripts.js"></script>
            

Each of the steps for the build process can be found in gruntfile.js, which is generated when the app is created. There are several build target options as well.

yeoman server

Yeoman also offers a built-in hosting server to test your app locally. LiveReload or simple file watching if you don’t have LiveReload also ensures that, when running the server, any changes to files in the app will automatically reload the browser with the new changes. By default, the port that it will run on is 3051. By running the following…

                # Run this for the non-built version
		yeoman server
                
                # Or this for the built version
                yeoman server:dist
			

A new browswer window will pop up with your app running. The server will also compile Coffee and Sass assets, and place them within a temp directory. So you don’t have to worry about compiling! There are several other server targets; be sure to check them out.

yeoman test

Mocha is a popular unit testing framework and is built into Yeoman. When a project is scaffolded, a test folder with an index.html file is created with Mocha and Chai for assertions. Simply run:

				yeoman test
			

Yeoman will then run Mocha against PhantomJS, which is a headless webkit browser that runs inside of Node.js. Then, you can feel free to add new tests into the index.html file. Unit testing can greatly enhance any application, and Yeoman makes the process as easy as possible. So there’s no excuses anymore! You can also use Jasmine, if you prefer, by passing --test-framework jasmine to the yeoman init command.

Learn more about using Jasmine in your projects here on Nettuts+.

yeoman install

To install new client libraries into your application, Yeoman uses a library, called Bower which is developed by some of the folks at Twitter.

Bower is a package manager for the web. Bower lets you easily install assets such as images, CSS and JavaScript, and manages dependencies for you.

Yeoman allows for the following commands:

				# Install any package(s) space delimited
				yeoman install jquery 
				
				# Uninstall things
				yeoman uninstall jquery
				
				# Update things
				yeoman update jquery
				
				# Installs underscore also since backbone depends on it
				yeoman install backbone
				
				# List out all of the installed things
				yeoman list
				
				# Go find stuff specifically based on a name
				yeoman lookup mocha
				
				# Go find stuff based on a keyword
				yeoman lookup underscore
			

Bower is an excellent addition to Yeoman, and solves the workflow issue of having to constantly retrieve libraries, when building apps. It also ensures that they are up to date. Here’s an example of how you could use Bower.

            bower install jquery
            bower install git://github.com/maccman/package-jquery.git
            bower install http://code.jquery.com/jquery-1.7.2.js
            bower install ./repos/jquery
            

The apps installed with bower will be stored in a component.json file at the root of the application.


Put it All Together

Let’s create a simple Backbone application from start to finish.

				# Create a new app
				yeoman init backbone
				
				# Create a new model and collection
				yeoman init backbone:model photo
				yeoman init backbone:collection photos
				
				# Create a some new views for flickr public photos
				yeoman init backbone:view flickr
				yeoman init backbone:view photoItem
				
				# WAVES HAND AND ADDS LOTS OF CODE
				# https://github.com/jcreamer898/yeomanbbapp
				yeoman server
				
				# Build ALL THE THINGS!
				yeoman build
				
				# Check out the new build man...
				yeoman server:dist
			

Wow, it’s almost too simple – and don’t forget that you can create Bash aliases to further shorten this code. The hardest part was creating the sample app! But, that’s the beauty of it. Yeoman takes you away from
the boilerplate code, and allows you to focus exclusively on the hard stuff!

If you’d like, take a look at the app to see how things went. I’d say pretty well.


Conclusion

Yeoman can rapidly speed up the development process for a new application. Rather than wasting time gathering libraries and writing boilerplate code, instead, type a few commands, and you’re up and running! Even better, it’s open source and written by a couple of guys you may know!

Enjoy!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • sammy

    YAY! another framework..that’s exactly what we needed!

    • http://www.jeffrey-way.com Jeffrey Way

      Not a framework in that way. This is a very cool release.

    • http://wouterj.nl Wouter J

      For what I have seen, I can’t test it, it is more a dependencies management tool plus. It can handle many depencies for JS and CSS and it can set up your development environment very quick + it has tools to minify/compress the code/images for a public environment.

    • Addy

      Yeoman is a set of tools defining an opinionated workflow, rather than a framework. We’re there to help simplify your process of getting frameworks/libraries, generating code for them, creating production-level apps with them etc :)

      Btw, great write-up guys!

    • Rob Iacobelli

      “YAY! I’m a technophobic programmer who hates progress! My very existence is absurdly ironic!” I just don’t get people on a site like this who are always hating when new frameworks, libraries, whatever come out (especially when they come with a tutorial and an open source product! )…This is how progress works, embrace it and have gratitude that people like paul and addy do so much research and work, then selflessly share the fruits of their labors for no compensation beyond knowing they are major contributors to the progress of web development (i assume). So take this build/stack/generator/tester/compiler kit and it’s components…from backbone to sass to jquery and try and do what this tutorial did manually without those ‘needless frameworks’ and see how that works out for you. seriously think about that effort. ( sorry for being a douche but this attitude is too common and too absurd, maybe naive but insulting to the devs who are cool enough to release this stuff)

  • Edvinas

    Is there a way to customize generated folder names? So ‘styles’ can become ‘css’ etc.?

    • Michael

      This is handled in Gruntfile.js in the compass section. In line 31 of the default Gruntfile, change temp/styles to temp/css and in line 32 change app/styles to app/css

    • Marcin

      I was willing to ask same question? How about integrating yeoman with ie. laravel? I would like to let yeoman do all its magic but I need it to be inside public folder. How can I do this? Can I modify grunt settings?

  • http://wouterj.nl Wouter J

    It is sad that there isn’t a way to install it on Windows… Not every webdev. has a unix computer and they want to use this very great tool (at least I think it is, I can’t install/test it) too…

    • http://www.jeffrey-way.com Jeffrey Way

      Per Addy’s comment, I’ve updated the article with a link to instructions for Windows users.

    • http://en.gs/ ENGS

      I feel sorry for people who have to develop on Windows; solely because setting up a good dev environment is so much harder… it’s getting there though, definitely!

  • http://addyosmani.com Addy

    For Windows users, I’d just like to say that you can now also use Yeoman on Windows. Here’s a guide by a member of the community http://www.decodize.com/css/installing-yeoman-front-end-development-stack-windows/ :)

  • Jared

    Can’t wait until it comes to windows.

  • http://sethmohit.com Mohit Seth

    I was introduced to Yeoman by my colleague few days back. I think we are going to try this. This is a very clean and efficient way to manage your codebase.

  • Maarten

    Using CodeKit to load Bootstrap modules etc into every single project. Works great also!

  • Joris Ooms

    Have been playing around with it today and I like it a lot. I can’t seem to generate a backbone project with jasmine as testing framework though.

    yeoman init backbone –test-framework jasmine always gives me a backbone project with mocha.

    Would be great if there’s a workaround for that, but apart from that issue it’s very fun to work with.

    Thanks for the article aswell, got me up and running.

  • wafto

    Me gusta!

  • Michael

    looks really interesting.. too bad im getting old and just don’t have the time to learn new things ;(

    • http://andymatthews.net/ Andy Matthews

      You’re only too old when you start making comments about how old you are. I’m 41 and learning new things about development almost every day. Don’t lay down and die yet my friend.

  • Lucas

    I know that yeoman is used for client side application but I’m wondering how to integrate with server side framework who usually has his own asset manager.

    For example: I’m using Yii Framework and it has his own assets manager. Unfortunately he is not good at minifying and contacting css/js files, compiling sass files with compass (there are some plugin but they now work quite well) so I thought I can use Yeoman for that (and for generating angular components, etc.). Could someone advice me how to use it with existing asset manager? Maybe I shouldn’t use asset manager in Yii at all?

    • Jintux

      Or maybe not even use Yii at all?

  • Hardik

    I think yeoman is perfect to build js apps rapidly. it was all needed for long time. yeah grunt was there but its kinda pain to manually lot of small tasks and i think yeoman is doing great job in rapidly creating things and allowing to build things so easily it just Rocks!

  • Jesus Bejarano

    When i run the code too install it, it does say that it was installed but when i try to run it with “yeoman x”, my computer still do not recognize it , btw i am working in ubuntu 12.04.

  • http://qawemlilo.rflab.co.za Que

    I would like to compile scripts of an existing backbone project to one script, can yeoman do this for me?

  • Tomasz

    Could be nice to integrate this framework with … Eclipse.

    There is a lot of tools that I use in development processes in other environments and in project management, time tracking etc.

    Having Yeoman perspective providing all this information that now you flush into the console would be great.

    Do you consider this direction?

  • Sandro

    This is a good way to save (20 minutes?) on the first day of your project.

    The cost is a dependency tree that you no longer control or understand, yet are beholden to.

    This may, or may not, be what you want.

    It is nice to have changes reflected immediately. Except when it isn’t, such as when a model or controller change breaks the view (perhaps with significant consequences, like a crash).

    There is a lot to learn here about what sorts of services and structures one will likely have to build into your application when it moves beyond trivial. Studying Yeoman’s source code will likely reveal some intelligent coding.

    It might be more fun, more informative, to do it yourself (how hard is it to create a Backbone model, or set up a test suite, or insert a jQuery plugin, really? And: don’t you want to know what is actually happening?). This is how you become better, how you find new ideas, how you move from being a slave to a system you don’t understand to an innovator.

    This may, or may not, be what you want.

    • http://andymatthews.net Andy Matthews

      You have a few options. Use Yeoman to generate, then quickly rename folders. Easy does it. Or you can edit the Grunt settings and make Yeoman generate the folders you want.

  • Nathan

    Sandro++

    IMHO, the creators of yeoman are probably the most brilliant minds in the industry, but there’s so much to be said for understanding how this all works.

    Coupling your app to a scaffolding tool that may or may not be here in 8 months seems a bit risky.

  • http://crpwebdev.com Caio Ribeiro Pereira

    This Yeoman is a powerful devtools, there are many useful features and each ones are just a combination of existing frameworks, like Bootstrap, Boilerplate and etc…

    Good work guys!

  • thucxuong

    I Think this is the best tool i’ve ever seen. It surprises me when i first watch the video. Man, so awesome !

  • http://blog.hizup.com Anton

    Very good article!
    Yeoman is really good?

  • http://www.linkedin.com/in/vgrigory vgrigory

    tried to install from my Ubuntu 12.04 (no VMs, …, just pure Ubuntu),got following error

    vgrigory@vgrigory:~$ curl -L get.yeoman.io | sh
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0
    100 5758 100 5758 0 0 4331 0 0:00:01 0:00:01 –:–:– 5623k
    sh: 20: [[: not found
    sh: 22: [[: not found
    Windows is not officially supported, currently.

    • ian

      Wow that comment just caught my eye. Really? You’re on Ubuntu and it tells you Windows is not supported? That’s funny.

      The instructions at yeoman.io say to use bash, not sh
      $ curl -L get.yeoman.io | bash

      Maybe that will solve the problem.

      You can look at the install.sh source here and see where the silly Windows error message is coming from.
      https://raw.github.com/yeoman/yeoman/master/setup/install.sh

  • http://www.jps.com.ve/ Saúl Solórzano

    Excellent! I’ve been looking forward to this release!

  • Chandra

    Unable to install yeoman, getting following error, pls help…
    > sudo npm install -g yeoman
    Password:
    npm http GET https://registry.npmjs.org/yeoman
    npm http 304 https://registry.npmjs.org/yeoman

    npm ERR! Error: No compatible version found: yeoman
    npm ERR! No valid targets found.
    npm ERR! Perhaps not compatible with your version of node?
    npm ERR! at installTargetsError (/usr/local/lib/node_modules/npm/lib/cache.js:488:10)
    npm ERR! at next_ (/usr/local/lib/node_modules/npm/lib/cache.js:438:17)
    npm ERR! at next (/usr/local/lib/node_modules/npm/lib/cache.js:415:44)
    npm ERR! at /usr/local/lib/node_modules/npm/lib/cache.js:408:5
    npm ERR! at Object.saved [as oncomplete] (/usr/local/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:147:7)
    npm ERR! You may report this log at:
    npm ERR!
    npm ERR! or email it to:
    npm ERR!
    npm ERR!
    npm ERR! System Darwin 12.1.0
    npm ERR! command “node” “/usr/local/bin/npm” “install” “-g” “yeoman”
    npm ERR! cwd /Users/veeracs
    npm ERR! node -v v0.6.8
    npm ERR! npm -v 1.1.0-2
    npm ERR! message No compatible version found: yeoman
    npm ERR! message No valid targets found.
    npm ERR! message Perhaps not compatible with your version of node?
    npm ERR!
    npm ERR! Additional logging details can be found in:
    npm ERR! /Users/veeracs/npm-debug.log
    npm not ok

    • Paul Asselin

      Getting same problem, did you solve it?

    • dewy666

      Make sure your nodejs and npm installations are both up to date.

  • Rigo

    Two things. Weirdest mascot ever. And impressive work. The build and test tools look incredible, a great advancement over grunt.js. I’m not sure I’ll use the scaffolding/dependency management other than the quickstart, but cool beans all the same.

  • Jhon

    I would really like a video tutorial for this article, BTW can this be used for developing in wordpress?

  • http://www.imstillreallybored.com Josh Bedo

    I’m pretty sure this will be an awesome tool and I definitely will use it. Seems very similar to SASS and Compass which I love how quick things can be done through command line.

  • Nick

    I’ve been hanging out for this!

    I currently use CodeKit for compiling SASS, concatenating, minifying, linting etc… and it works great. What I like about this tool is that you get all of that with a few extras.

    I think a command line tool using generators is gonna be easier than CodeKits “Frameworks” plus you get a built-in hosting server.

    To be fair… I haven’t used it yet but looks promising.

  • Fritz

    The tool is already great but the illustrations are just AWESOME! :)

  • ian

    Wow this is awesome! And though the post isn’t about Chocolatey, it’s awesome too! All of the packages I’ve painstakingly installed and configured manually in the past several weeks were re-installed to C:\Chocolatey in a few minutes and added to the path variable and everything is working great.

    But back to the topic, yeoman seems really promising and cool. It is going to be fun figuring out how to create my own generators and build scripts.

    Thank you for the intro.

  • Calvin

    Promising tool and a great right up!
    I’m surely being dim here, but it says default build will minify and concat scripts, right? I’m sure I’m missing something, but I can’t get this working.
    eg. in index.html I reference 2 separate js files

    I want index.html in dist of

    or better yet – as per the versioning – I want (something like)

    But it doesn’t happen – index.html still has 2 script tags, not the single for the concatenated one. I do get the foobar.js in the scripts folder, but also get the seperate foo.js and bar.js – so it’s sort of half way there. Is it actually meant to rewrite, or is that bit manual?? Am I meant to add anything to the Grunt.js? (I tried…what works with grunt alone breaks yeoman).

    Any ideas?

  • Calvin

    OK, the code snippets don’t work in your comments then :-|
    I was just trying to see how to get 2 script files concatenated and the script src path in index.html changed automatically to that concatenated script…

  • http://www.stopbrain.com erictherobot

    awesome. thanks for making my dull day exciting!

  • h4rrydog

    Great article and great tool! Just installed it and I’m playing around with it — seems like it will save me lots of time.

    Btw, I think there is a typo in the yeoman server section. The port for the server on my install is ’3501′ instead of port ’3051′ in the article.

    Thanks again for the great work!

  • http://meekgeek.com Joel Caballero

    What color profile are they using for the TOC? I love it :)