JavaScript Modules
JavaScript modules allow developers to split their code into smaller, reusable pieces, making it easier to manage large codebases. Modules provide a way to organize functionality and share code between different files, which is especially useful for maintaining cleaner and more maintainable code.
In simple terms, a JavaScript module is a piece of code that is contained in a separate file, making it easier to organize and reuse. Modules can export functions, objects, or variables, and other modules can import these exports to use them. This helps in making your application more modular, which in turn improves readability, maintainability, and scalability.
ES6 modules, also known as ECMAScript modules (ESM), were introduced in ECMAScript 2015 and are now widely supported in modern browsers and Node.js. ES6 modules allow you to export and import variables, functions, or classes between JavaScript files.
There are two ways to export from a module in ES6: named exports and default exports.
With named exports, you can export multiple variables or functions from a module.
math.js (module file)
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
app.js (importing module)
import { add, subtract } from './math.js';
console.log(add(2, 3)); // Output: 5
console.log(subtract(5, 2)); // Output: 3
In this example, the math.js
module exports two functions, add
and subtract
. In app.js
, we import them and use them to perform operations.
You can also use default exports to export a single value, such as a function, class, or object.
math.js (module file)
export default function multiply(a, b) {
return a * b;
}
app.js (importing module)
import multiply from './math.js';
console.log(multiply(2, 3)); // Output: 6
Here, we have a default export in math.js
. Since there is only one export, we use the import
statement without curly braces.
You can import modules using the import
statement. The syntax for importing differs slightly depending on whether you are using named or default exports.
import { exportName } from './moduleName';
import exportName from './moduleName';
You can also import all the exports from a module as an object.
import * as math from './math.js';
console.log(math.add(2, 3)); // Output: 5
console.log(math.subtract(5, 2)); // Output: 3
Let’s see how you can structure a JavaScript project using modules for better code organization. A common approach is to have different files for different features of the application.
/project
/math
add.js
subtract.js
multiply.js
/app
main.js
index.js
add.js (module file)
export const add = (a, b) => a + b;
subtract.js (module file)
export const subtract = (a, b) => a - b;
multiply.js (module file)
export const multiply = (a, b) => a * b;
main.js (importing module)
import { add } from '../math/add.js';
import { subtract } from '../math/subtract.js';
import { multiply } from '../math/multiply.js';
console.log(add(2, 3)); // Output: 5
console.log(subtract(5, 2)); // Output: 3
console.log(multiply(2, 3)); // Output: 6
In this example, each operation (addition, subtraction, multiplication) has its own module file. The main.js
file imports only the relevant functions and uses them.
While ES6 modules are the standard for modern JavaScript, CommonJS modules are still widely used, especially in server-side applications like Node.js. CommonJS modules are used with require
and module.exports
for importing and exporting modules.
// math.js
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
module.exports = { add, subtract };
// app.js
const math = require('./math');
console.log(math.add(2, 3)); // Output: 5
console.log(math.subtract(5, 2)); // Output: 3
With CommonJS, we use module.exports
to export functions or objects and require
to import them.
Feature | ES6 Modules | CommonJS Modules |
---|---|---|
Syntax | import / export |
require() / module.exports |
Asynchronous Support | No | Yes |
Loading | Static (determined at compile time) | Dynamic (resolved at runtime) |
Usage | Browser & Node.js (with Babel) | Mainly Node.js |
While ES6 modules are now the standard for modern JavaScript, CommonJS modules are still useful, especially in Node.js environments.
To use ES6 modules in the browser, you need to include the type="module"
attribute in the <script>
tag. This tells the browser to treat the JavaScript file as a module.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Modules</title>
</head>
<body>
<script type="module">
import { add } from './math.js';
console.log(add(2, 3)); // Output: 5
</script>
</body>
</html>
By adding type="module"
, you enable the browser to recognize and handle JavaScript modules, which allows you to use import
and export
statements directly in the browser.