April 2018
Beginner
536 pages
13h 21m
English
If we compile the external module that we defined in the ES6 modules section into an CommonJS module (using the flag --compile commonjs), we will obtain the following CommonJS module:
var UserModel = (function () {
function UserModel() {
//...
}
return UserModel;
})();
module.exports = UserModel;
As we can see in the preceding code snippet, the CommonJS module definition syntax is almost identical to that of the legacy external module syntax. The main difference is the usage of the module object and its exports property instead of the exports keyword.
The preceding CommonJS module can be loaded natively by a Node.js application using the import keyword and the require function:
import UserModel = require('./UserModel'); ...