ES6, CommonJS or Amd

TL;DR doesn’t mather - pick one and use that.

ES-6

ES6 modules is the future. At some point this will be a part of the language, and then I see no reason not to use that. But we are not there yet. ES-6 modules seems pretty to me. Maybe because they look so much like python. I like them - have never used them for real though.

<code class="javascript">import { Rect } from "Boxes";
function Square(width) {
  this.width = width;
  this.height = width;
}
Square.prototype = new Rect();
export Square;
</code>

CommonJs

CommonJs is the way to do modules in node-land. It can be used in the browser as well, if you run it through browserify.

<code class="javascript">var Rect = require("Boxes").Rect;
function Square(width) {
  this.width = width;
  this.height = width;
}
Square.prototype = new Rect();
exports.Square = Square;
</code>

AMD

The only module format I have used on larger (frontend-)projects. It works out of the box in the browser, if you have an amd loader present. (Most people use require.js).

<code class="javascript">define("segment",
  ["Boxes"],
  function(Boxes) {
    function Square(width) {
      this.width = width;
      this.height = width;
    }
    Square.prototype = new Boxes.Rect();
    return Square;
  });
</code>

Which to choose?

If you were told to only decide upon looking at these three extracts of code - you would probably choose the es6 option. I know I would. Too bad that there are so many other considerations and constraints.

If I were able to choose freely - and not think about legacy stuff - I would choose es6, a transpiler to make my es6-modules into commonjs ones, and then use browserify to create a browser version of that…. But then again - I love complex build scripts to make simple stuff….