Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Topologically sort directed acyclic graphs (such as dependency lists) in node.js
Branch: master
Failed to load latest commit information.
benchmark benchmark: Rerun benchmarks.
build Rebuild code with correct formatting.
src src: Add some explanatory comments
test test: Shuffle the add order for the more complex test
.eslintrc run eslint on Gruntfile
.gitattributes Grunt building
.gitignore Fix gitignore
.npmignore Update ignore files
Gruntfile.js build: Explicitly name the module "Toposort", so browsers can referen…
bower.json Bump version to 1.0.0-beta
index.js Strip the 'Created by` comments WebStorm adds in.
package.json Bump version to 1.0.1


Build Status Dependency Status

Sorting directed acyclic graphs, for Node.js, io.js and the browser This was originally done by Marcel Klehr. Why not checkout his original repo?


There are a few ways for installing Toposort. Here are them:

  • Via NPM: npm install toposort-class
  • Via Bower: bower install toposort
  • Via Git: git clone git://
  • Direct download (Minified) for use in the browser


Let's say you have the following dependency graph:

  • Plugin depends on Backbone and jQuery UI Button;
  • Backbone depends on jQuery and Underscore;
  • jQuery UI Button depends on jQuery UI Core and jQuery UI Widget;
  • jQuery UI Widget and jQuery UI Core depend on jQuery;
  • jQuery and Underscore don't depend on anyone.

Now, how would you sort this in a way that each asset will be correctly placed? You'll probably need the following sorting:

  • jQuery, jQuery UI Core, jQuery UI Widget, jQuery UI Button, Underscore, Backbone, Plugin

You can achieve it with the following code, using toposort-class:

var Toposort = require('toposort-class'),
    t = new Toposort();

t.add("jquery-ui-core", "jquery")
 .add("jquery-ui-widget", "jquery")
 .add("jquery-ui-button", ["jquery-ui-core", "jquery-ui-widget"])
 .add("plugin", ["backbone", "jquery-ui-button"])
 .add("backbone", ["underscore", "jquery"]);


/* Will output:
 * ['jquery', 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-button', 'underscore', 'backbone', 'plugin']
 * And you're done.


CommonJS (Node.js and io.js):

var Toposort = require('toposort-class'),
    t = new Toposort();

Browser with AMD:

define("myModule", ["Toposort"], function(Toposort) {
    var t = new Toposort();

Browser without AMD:

var t = new window.Toposort();

or whatever global object there is instead of window.


.add(item, deps)

  • {String} item - The name of the dependent item that is being added
  • {Array|String} deps - A dependency or list of dependencies of item

Returns: {Toposort} The Toposort instance, for chaining.


Returns: {Array} The list of dependencies topologically sorted.

This method will check for cyclic dependencies, like "A is dependent of A".


Returns: {Toposort} The Toposort instance, for chaining.

Clears all edges, effectively resetting the instance.


Reference to the Toposort constructor.


MIT License

Something went wrong with that request. Please try again.