Introducci贸n a Node.js
Introducci贸n Node.js
Instalaci贸n y configuraci贸n del entorno de Node.js
Primer proyecto con Node.js
Quiz: Introducci贸n a Node.js
M贸dulos y gesti贸n de paquetes
Tipos de M贸dulos en Node.js
Gesti贸n de Paquetes con NPM
Creaci贸n de un Paquetes con NPM
Publicaci贸n de Paquetes con NPM
Quiz: M贸dulos y gesti贸n de paquetes
M贸dulos nativos en Node.js
Introducci贸n al M贸dulo FS de Node.js
Leer y escribir archivos en Node.js
M贸dulo fs: Implementar transcripci贸n de audio con OpenAI
M贸dulo Console: info, warn, error, table
M贸dulo Console: group, assert, clear, trace
M贸dulo OS: informaci贸n del sistema operativo en Node.js
M贸dulo Crypto: cifrado y seguridad en Node.js
M贸dulo Process: manejo de procesos en Node.js
Timers: setTimeout, setInterval en Node.js
Streams: manejo de datos en tiempo real en Node.js
Buffers: manipulaci贸n de datos binarios en Node.js
Quiz: M贸dulos nativos en Node.js
Servidores con Node.js
HTTP: fundamentos de servidores en Node.js
Servidor nativo y streaming de video en Node.js
You don't have access to this class
Keep learning! Join and start boosting your career
Modules in NodeJS are fundamental for building robust and maintainable applications. Like Lego pieces, these encapsulated code blocks with specific functionalities allow us to create complex structures from simple and reusable components. This modularity not only facilitates code maintenance, but also avoids conflicts in large-scale applications.
Modules in NodeJS are encapsulated code blocks that fulfill a specific function. This encapsulation allows us to reuse the code in different parts of our application, facilitate its maintenance and avoid conflicts in complex projects. There are different types of modules that we can implement in our applications:
In NodeJS we mainly find two module systems:
CommonJS (CJS): it is the original NodeJS system, which uses:
require()
to import and module.exports
to export .
.js
ES Modules (ESM): It is the modern system, compatible with ECMAScript 6+:
import
and export
.mjs
(although .js
can also be used with proper configuration)In addition to module systems, there are different types depending on their origin:
Native modules: come built into NodeJS:
fs
: for file system handling.http
: for creating HTTP servicespath
: for accessing file pathsos
: to interact with the operating systemThird-party modules: They are installed from NPM (Node Package Manager):
npm install module-name
The implementation of modules varies according to the system we use. Let's see practical examples of each one:
To create a module with CommonJS, we must export the functionalities we want to share:
// math.jsfunction add(a, b) { return a + b;}
function subtract(a, b) { return a - b;}
module.exports = { add, subtract };
To use this module in another file:
// main.jsconst math = require('./math');
console.log(`Sum: ${math.add(5, 3)}`);console.log(`Subtract: ${math.subtract(5, 3)}`);
When running node src/main.js
, we will get:
Addition: 8Subtraction: 2
To create a module with ES Modules, we use the export
syntax:
// math.mjsexport function multiply(a, b) { return a * b;}
export function divide(a, b) { return a / b;}
To use this module:
// main.mjsimport { multiply, divide } from './math.mjs';
console.log(`Multiplication: ${multiply(5, 3)}`);console.log(`Divide: ${divide(6, 2)}`);
When running node src/main.mjs
, we will get:
Multiplication: 15Division: 3
Native modules are available without additional installation:
// native.jsconst fs = require('fs');
const data = fs.readFileSync('example.txt', 'utf8');console.log(`Filecontent: ${data}`);
If the file example.txt
contains `Hello World`, when we run this code we will see:
File content: Hello World
To use third-party modules, we must first install them:
npm install is-odd
Then we can use them in our code:
// third-party.jsconst isOdd = require('is-odd');
console.log(isOdd(1)); // trueconsole.log(isOdd(3)); // true
The choice between CommonJS and ES Modules depends on several factors:
The most important thing is to maintain consistency throughout the project, choosing a single module system to avoid confusion and compatibility issues.
Code modularization is a fundamental practice in NodeJS development that will allow you to build more organized, maintainable and scalable applications. I encourage you to experiment with different types of modules and find the approach that best suits your needs. Have you worked with modules in NodeJS? Share your experience and doubts in the comments section.
Contributions 4
Questions 0
Want to see more contributions, questions and answers from the community?