Modules
JavaScript modules are not new. In fact, there were have been libraries that support modules for some time now. ES6, however, offers built-in modules. Traditionally, JavaScript's major use was on browsers, where most of the JavaScript code was either embedded or small enough to manage without much trouble. Things have changed. JavaScript projects are now on a massive scale. Without an efficient system of spreading the code into files and directories, managing code becomes a nightmare.
ES6 modules are files. One module per file and one file per module. There is no module keyword. Whatever code you write in the module file is local to the module unless you export it. You may have a bunch of functions in a module, and you want to export only a few of them. You can export module functionality in a couple of ways.
The first way is to use the export
keyword. You can export any top-level function
, class
, var
, let
, or const
.
The following example shows a module inside server.js
where we export...