JavaScript/AngularJS adventures of a Java guy Part 2 – npm and Bower

bower

Tooling

In Part 1, I talking about project structure and only hinted at development tools. Let’s talk about some dependency tools now. In the next blog post, we’ll cover even more tools.

As a Java developer, I’ve already used Git and Github quite a lot. Not surprisingly, it works well with JavaScript too. If you haven’t used it, now is a good time to start.

Tools for Dependencies

npm and Bower

Do you know how you tell Maven that your project has a dependency, say, spring-core 4.0.3-RELEASE? You put a few lines in your pom.xml, and maven will download the internet for you. Just kidding, it will grab that dependency and all the things that it depends on. So, you don’t have to go to a web site, download a zip/jar, and paste them into your project. Wouldn’t it be nice if JavaScript had that?

It does. But it’s not just one tool like maven. The JavaScript ecosystem is using tools developed for Node.js. Node.js let you write server side JavaScript. I prototyped a few RESTful web services last summer for a client start up last summer using Node.js. It was fun and fast. I just wouldn’t use it in production. Even if your back end is in Java using Spring, you should install it just to get the Node Package Manager, npm (read the FAQ and you’ll see that some snarkasaurus says it doesn’t mean this). Go to the Node.js website, and press the big green install button. On my Macbook, it will live at /usr/local/bin/npm. npm will install binaries along with node modules.

Another package manager is Bower which is installed via npm.

The -g flag means install it globally and not just for your current project.

To use bower, you need to have a bower.json config file in your project. You can create one yourself, or let Bower help you create one interactively.

Inspect bower.json and you will see this:

Notice that when you did the lookup that bower let you know what repository you will be getting the code from. This is important because anyone can publish code that bower can grab. See that last question from the init action that helped you from accidentally publishing to the bower repository? So OK, lesson learned. We should know where the code is coming from.

You’ve probably noticed that the code is saved in bower_components/ You can change this by creating a .bowerrc file in your home directory or current project. Here is an example that saves it in a maven friendly location:

Let’s add some more, this time with a testing library.

Here is the bower.json excerpt now. (Of course, when you run this, the versions might be newer).

In your HTML, you reference the downloaded files like this.

Next

You know those maven plugins that will copy files to different locations, or create a buildnumber, or that maven plugin you wrote yourself that did that thing? In the next blog post I’ll talk about a tool that will do this grunt work for you.

2 thoughts on “JavaScript/AngularJS adventures of a Java guy Part 2 – npm and Bower”

  1. Hi,

    Thank you for this series of posting around angularJS coming from a java background. It’s also my case.
    I am actually going through your blog as we speak and was wondering if you had a date in mind for Part 3 ?

    More specifically I was curious to see whether you configured grunt on top an ng-project that you got using angular-seed (as in Part1) or whether you adopted the easy way out, which is to use the yeoman project and their angular generator instead.

    Thanks,

    1. Thanks Mehdi,
      I just need about 10 minutes to proof read Part 3, but I’ve been buried in a project.
      I’ll do it soon.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.