187

How do you layout a folder structure for a large and scaleable AngularJS application?

1

5 Answers 5

255

enter image description here

Sort By Type

On the left we have the app organized by type. Not too bad for smaller apps, but even here you can start to see it gets more difficult to find what you are looking for. When I want to find a specific view and its controller, they are in different folders. It can be good to start here if you are not sure how else to organize the code as it is quite easy to shift to the technique on the right: structure by feature.

Sort By Feature (preferred)

On the right the project is organized by feature. All of the layout views and controllers go in the layout folder, the admin content goes in the admin folder, and the services that are used by all of the areas go in the services folder. The idea here is that when you are looking for the code that makes a feature work, it is located in one place. Services are a bit different as they “service” many features. I like this once my app starts to take shape as it becomes a lot easier to manage for me.

A well written blog post: http://www.johnpapa.net/angular-growth-structure/

Example App: https://github.com/angular-app/angular-app

13
  • 25
    are you answering your own question right after posting it?
    – Jakub
    Aug 30, 2013 at 22:34
  • 35
    @Jakub There's an option called "answer your own question contributing to wiki" when you create the question. Aug 30, 2013 at 22:41
  • 3
    @MichaelCalkins, I like the idea. I just checked out some of your youtube tuts on angular too, nice
    – Ronnie
    Aug 30, 2013 at 22:50
  • 5
    @Ronnie I just remember this being one of the hardest learning curve steps to AngularJS so hopefully it helps people out. I think I went through like 10 different layouts after reading blogs and what not. Aug 30, 2013 at 22:52
  • 5
    Here is a community-driven angularJS best practices page. Their file structure suggestion is a combination of both of your ideas in two examples. I like the second example better as it is more behind the idea of sorting by feature.
    – John
    Apr 22, 2014 at 6:26
32

After building a few applications, some in Symfony-PHP, some .NET MVC, some ROR, i've found that the best way for me is to use Yeoman.io with the AngularJS generator.

That's the most popular and common structure and best maintained.

And most importantly, by keeping that structure, it helps you separate your client side code and to make it agnostic to the server-side technology (all kinds of different folder structures and different server-side templating engines).

That way you can easily duplicate and reuse yours and others code.

Here it is before grunt build: (but use the yeoman generator, don't just create it!)

/app
    /scripts
            /controllers
            /directives
            /services
            /filters
            app.js
    /views
    /styles
    /img
    /bower_components
    index.html
bower.json

And after grunt build (concat, uglify, rev, etc...):

    /scripts
        scripts.min.js (all JS concatenated, minified and grunt-rev)
        vendor.min.js  (all bower components concatenated, minified and grunt-rev)
    /views
    /styles
        mergedAndMinified.css  (grunt-cssmin)
    /images
    index.html  (grunt-htmlmin)
2
  • 1
    where should it be the configuration of routes block of code? angular.module('myApp').config(function($routeProvider) { ... });
    – sports
    Sep 14, 2014 at 18:28
  • 1
    +1 Also, you can customize where yeoman puts your stuff. IMHO, sorting by feature/module in big apps is better since you can more easily reuse a feature in other apps.
    – Tivie
    Oct 23, 2014 at 23:54
30

I like this entry about angularjs structure

It's written by one of the angularjs developers, so should give you a good insight

Here's an excerpt:

root-app-folder
├── index.html
├── scripts
│   ├── controllers
│   │   └── main.js
│   │   └── ...
│   ├── directives
│   │   └── myDirective.js
│   │   └── ...
│   ├── filters
│   │   └── myFilter.js
│   │   └── ...
│   ├── services
│   │   └── myService.js
│   │   └── ...
│   ├── vendor
│   │   ├── angular.js
│   │   ├── angular.min.js
│   │   ├── es5-shim.min.js
│   │   └── json3.min.js
│   └── app.js
├── styles
│   └── ...
└── views
    ├── main.html
    └── ...
3
  • 1
    The question is about folder structure and though the link is interesting in other ways, it doesn't bring anything new to the table for this question.
    – JohnC
    Apr 7, 2014 at 18:10
  • This is used by Yeoman angular generator
    – EdgarT
    Apr 13, 2015 at 23:35
  • So you've organised the folder by type of scripts.
    – Tudor
    Jun 5, 2015 at 14:04
4

There is also the approach of organizing the folders not by the structure of the framework, but by the structure of the application's function. There is a github starter Angular/Express application that illustrates this called angular-app.

1
  • This is often a better approach for very large projects. Sadly we are still stuck with folder structures ... tagging would be better so it would not matter so much and we could have multiple views depending on the tags. Apr 21, 2016 at 15:06
3

I'm on my third angularjs app and the folder structure has improved every time so far. I keep mine simple right now.

index.html (or .php)
/resources
  /css
  /fonts
  /images
  /js
    /controllers
    /directives
    /filters
    /services
  /partials (views)

I find that good for single apps. I haven't really had a project yet where I'd need multiple.

3
  • You will have problems in maintaining and introducing new features later. It is always a good practice to have a folder structure with features and components related to that feature under the its folder. Oct 10, 2015 at 15:52
  • Am I right in thinking that Angular JS has no standard project folder structure or project template, like an asp.net web project or a windows desktop application ?
    – user3393933
    Dec 5, 2017 at 9:37
  • 1
    @dotNetBlackBelt that is correct. There is no standard when it comes to angular. Since posting this, I've made changes to how I setup my folders. My last project I went more or less what the top answer of this OP is
    – Ronnie
    Dec 5, 2017 at 16:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.