In ES5 ( released in 2009 ) JavaScript had no keywords like import, include, or require.
ES6 saves the day ( released in 2015 ) proposing the import keyword ( https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/import ), and now all modern browsers support this.
If you use Babel 6.18.0 and transpile with ES2015 option only
import myDefault from "my-module";
you will get require
again.
"use strict";var _myModule = require("my-module");var _myModule2 = _interopRequireDefault(_myModule);function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
This is because require
means the module will be loaded from Node.js. Node.js will handle everything from system level file read to wrapping functions into the module.
Because in JavaScript functions are the only wrappers to represent the modules.
I'm a lot confused about CommonJS and AMD?
Both CommonJS and AMD are just two different techniques how to overcome the JavaScript "defect" to load modules smart.
AMD
CommonJS:
Example
upper.js file
exports.uppercase = str => str.toUpperCase()
main.js file
const uppercaseModule = require('uppercase.js')uppercaseModule.uppercase('test')
Summary
Resources: