JavaScript Modules

Learn about JavaScript modules

By the end of this lesson, we’ll be able to apply ES6 modules to modularize our scripts.

Modules

Note: This lesson uses CommonJS modules throughout.

Modules allow developers to organize their codebase within separate files or components for better reusability, flexibility, shareability, and overall maintenance. Multiple modules can be composed together to create an application.

ECMAScript modules, which are a newer feature in JavaScript, are defined with the use of the import and export keywords.

Although ECMAScript modules are the official standard format to package JavaScript code, they’re still considered an experimental Node.js feature as of July 2020. This is because the Node.js team is still working on ensuring backward compatibility and support for both module systems.

CommonJS, which is the legacy system, uses the module.exports syntax for exports and the require() function for imports:

Get hands-on with 1200+ tech skills courses.