ES6 Modules
When writing JavaScript with Node.js, the ability to import modules using the built-in require()
function has long been used. As this feature was useful, many frontend developers began making use of it by pre-processing their JavaScript with compilers such as Babel. A JavaScript pre-compiler processes code that normally wouldn't work on most browsers and generates a new JavaScript file that is compatible.
As there was a large demand for an import style function in JavaScript, it was eventually added to the language in the ES6 version. At the time of writing, the latest version of most browsers is almost completely ES6 compatible. Yet the use of import
can't be taken for granted, as many devices will continue to run older versions for years.
The rapid standardization of ES6 shows us that, going forward, ES6 imports will be the most popular method.
In the last chapter, we made use of the Node.js require
method for importing a module. For example, take this...