mirror of
https://github.com/andwati/andwati.com.git
synced 2026-10-19 09:52:52 +00:00
edited blogs
This commit is contained in:
@@ -9,15 +9,14 @@ tags = ["javascript", "node"]
|
||||
|
||||
Modules are the building blocks of modern JavaScript applications, allowing for the encapsulation of code into reusable components. Two primary systems govern the exportation of these modules: export default in ES6 and module.exports in CommonJS.
|
||||
|
||||
|
||||
|
||||
Javascript is a popular programming language that can run both in the browser and on the server. However, different environments may have different ways of organizing and importing Javascript modules. In this article, we will compare two common methods of exporting and importing modules: **export default** and **module.exports**.
|
||||
|
||||
|
||||
## What are modules?
|
||||
|
||||
Modules are reusable pieces of code that can be separated into different files. Modules can export values, functions, classes, or objects that can be imported by other modules. This way, modules can avoid polluting the global scope and enable code reuse and modularity.
|
||||
|
||||
## How to export modules in Node.js?
|
||||
|
||||
Node.js is a runtime environment that allows Javascript to run on the server. Node.js uses a module system called CommonJS, which is based on the require and module.exports syntax.
|
||||
|
||||
To export a module in Node.js, you can assign the value that you want to export to the module.exports object. For example:
|
||||
@@ -53,6 +52,7 @@ console.log(person.age); // 25
|
||||
Note that Node.js will cache the modules that are imported with require, so if you import the same module multiple times, you will get the same instance of the module.
|
||||
|
||||
## How to export modules in ES6?
|
||||
|
||||
ES6 is the version of Javascript that introduced many new features, including a native module system. ES6 modules use the `import` and `export` syntax.
|
||||
|
||||
To export a module in ES6, you can use the `export` keyword followed by the declaration of the value that you want to export. You can also use the export keyword with curly braces to export a list of named values. For example:
|
||||
@@ -98,9 +98,11 @@ import { person as Alice } from "./my-module.js";
|
||||
console.log(Alice.name); // Alice
|
||||
console.log(Alice.age); // 25
|
||||
```
|
||||
|
||||
Note that ES6 modules are imported and executed only once, so if you import the same module multiple times, you will get the same instance of the module.
|
||||
|
||||
## What is the difference between export default and module.exports?
|
||||
|
||||
One of the main differences between export default and module.exports is that export default is an ES6 feature, while module.exports is a CommonJS feature. Therefore, they are not compatible with each other by default. However, some tools like Babel or Webpack can transpile ES6 modules to CommonJS modules, or vice versa, to make them work together.
|
||||
|
||||
Another difference is that export default can only export one value per module, while module.exports can export any value, including multiple values. For example:
|
||||
@@ -172,6 +174,7 @@ console.log(person.age); // 30
|
||||
```
|
||||
|
||||
To import a module.exports value, you can use the require function and assign any name to the imported value. For example:
|
||||
|
||||
```javascript
|
||||
// main.js
|
||||
// import a module.exports value
|
||||
@@ -189,4 +192,5 @@ const person = new Person("Bob", 30);
|
||||
console.log(person.name); // Bob
|
||||
console.log(person.age); // 30
|
||||
```
|
||||
Node’s JavaScript has diverged from that of the browser. Understanding both CommonJS modules and ES6 modules is your first step in becoming a 10X developer. Happy coding!
|
||||
|
||||
Node’s JavaScript has diverged from that of the browser. Understanding both CommonJS modules and ES6 modules is your first step in becoming a 10X developer. Happy coding!
|
||||
|
||||
Reference in New Issue
Block a user