ES6
Module
module is a piece of reusable code, one file per module
import
Modules are imported with either absolute or Relative references and must start with either “/”, “./”, or “../”
export
export {sumAll, substractAll, divideAll, multiplyAll, findModulus}
import {sumAll, substractAll} from "./module"
export {sumAll, substractAll, divideAll, multiplyAll, findModulus}
import * as math from "./module"
//use math.sumAll, math.substractAll
export default
can be used in place of export when you wish to export only one type
one can have only one default export per file
export default {sumAll, subtractAll, divideAll, multiplyAll, addFive};
import math from './math.js';
//use math.submAll, math.substractAll
Module Example
//index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="module" src="../src/app.js"></script>
</body>
</html>
//helper.js
export const print = (message) => log(message, new Date());
export const log = (message, timestamp) =>
console.log(`${timestamp.toString()}: ${message}`)
import {print, log} from './helper.js';
console.log(print("Hello World!"));
CommonJS
const print = (message) => log(message, new Date());
const log = (message, timestamp) =>
console.log(`${timestamp.toString()}: ${message}`)
module.exports = {print, log}
const { log, print } = require('./helper')
console.log(print("Hello World!"));