JavaScript Modules

JavaScript modules are pieces of code that can be imported into other JavaScript files. They allow developers to organize code into smaller, reusable components that can be used in other projects. Modules are commonly used to create libraries, frameworks, and plugins.

Why should you use it?

  • Modules make it easier to organize code into smaller, reusable components.
  • Modules help to prevent code duplication and reduce the amount of code that needs to be written.
  • Modules make it easier to share code between projects and developers.

ES6 Modules

ES6 (ECMAScript 6) introduced a new way to work with modules. In this tutorial, we will learn about the different ways to use modules in JavaScript, and how to use them in your projects.

ES6 modules are a way to organize code in JavaScript. They are the modern way to write code that is more structured and organized. Modules allow us to split our code into multiple files, and each file can have its own scope. This helps us to keep our code organized and easier to maintain.

Importing and Exporting Modules

The main way to use modules is to import and export them. We can import a module from another file, and then use it in our code. We can also export a module from our file, so that other files can use it.

Let's look at an example. In the code block below, we have two files: index.js and add.js. The index.js file imports the add.js file, and then uses the add() function from it.

index.js
add.js
Run in Compiler
import add from './add.js';

console.log(add(1, 2));

In the add.js file, we have a function called add(). This function takes two numbers and returns the sum of them. We then export this function, so that it can be used in other files.

export function add(a, b) {
  return a + b;
}

export function subtract(a, b) {
  return a - b;
}

We can also export multiple functions from a file. In the code block below, we have a file called math.js which exports two functions: add() and subtract(). We can then import these functions into another file, and use them.

constants.js
Run in Compiler
export const PI = 3.14;

We can also import and export variables. In the code block below, we have a file called constants.js which exports a variable called PI. We can then import this variable into another file and use it.

export default function add(a, b) {
  return a + b;
}

Default Exports

We can also use default exports. This allows us to export a single value from a file without having to specify a name for it. In the code block below, we have a file called math.js which exports a function called add() as the default export. We can then import this function into another file and use it.

export function add(a, b) {
  return a + b;
}

export function subtract(a, b) {
  return a - b;
}

Named Exports

We can also use named exports. This allows us to export multiple values from a file, and specify a name for each one. In the code block below, we have a file called math.js which exports two functions: add() and subtract(). We can then import these functions into another file and use them.

export function add(a, b) {
  return a + b;
}

export function subtract(a, b) {
  return a - b;
}

Conclusion

In this tutorial, we learned about the different ways to use modules in JavaScript. We learned how to import and export modules, and how to use default and named exports. We also saw some examples of how to use modules in our projects.

CommonJS Modules

JavaScript Modules are used to organize code and separate it into different files. This makes it easier to maintain and modify the code. Modules can be imported and used in other files. This is done using the require() and module.exports functions.

The require() function is used to import a module from another file. The module.exports is used to export a module from a file. This allows other files to import and use the module.

The require() function takes a single argument, which is the path to the module that is being imported. The module.exports function takes an argument, which is the module that is being exported.

To demonstrate how to use the require() and module.exports functions, let's take a look at the following example.

index.js
module.js
Run in Compiler
const myModule = require('./module.js');

console.log(myModule.foo + ' ' + myModule.bar);

In the example above, we have two files, index.js and module.js. The index.js file is the entry point for the application. It imports the module.js file using the require() function. The module.js file exports the myModule object using the module.exports function. The myModule object has two properties, foo and bar.

When the index.js file is run, it will import the myModule object from the module.js file. The myModule object will be available in the index.js file and can be used to access the foo and bar properties. The output of the code will be foo bar.

1