Difference between module type in tsconfig.json

node.jsAngular

node.js Problem Overview


In tsconfig.json

{
    "compilerOptions": {
     "target": "es5",
     "module": "commonjs",
     "moduleResolution": "node",
     "sourceMap": true,
     "emitDecoratorMetadata": true,
     "experimentalDecorators": true,
     "removeComments": false,
     "noImplicitAny": false
  }
}

I cannot understand the difference between following module types:

> "commonjs", "amd", "umd", "system", "es6", "es2015", "none"

Question: Which value should I use and when should I use it?

node.js Solutions


Solution 1 - node.js

CommonJS pattern (or nodejs):

var someOtherFunction = require('./someOtherFunction.js');
exports.add = function() {
    var sum = 0, i = 0, args = arguments, l = args.length;
    while (i < l) {
        sum += args[i++];
    }
    return sum;
}

ES6 pattern:

import someOtherFunction from './someOtherFunction.js';

export function add() {
    var sum = 0, i = 0, args = arguments, l = args.length;
    while (i < l) {
        sum += args[i++];
    }
    return sum;
}

AMD pattern:

define(['someOtherFunction'], function () {
    return function () {
        var sum = 0, i = 0, args = arguments, l = args.length;
        while (i < l) {
            sum += args[i++];
        }
        return sum;
    };
});

Asynchronous Module Definition (AMD) is the most popular for client-side code, while node.js modules (an extension to CommonJS Modules/1.1) is the leading pattern in server-side environments.

Universal Module Definition (UMD) is a set of boilerplate recipes that attempt to bridge the differences between AMD and node.js, allowing engineers to author their code bases in a single format, rather than author in both formats or convert to the other format in a build step.

ES5 is the normal JavaScript that you used to see.

You'd be using ES6 for Angular2, also known as ECMAScript 2015.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser7324396View Question on Stackoverflow
Solution 1 - node.jsMiladView Answer on Stackoverflow