Spiegazione del sistema dei moduli di Node.js
Esplori i moduli CommonJS, i moduli ES e il modo in cui importare ed esportare funzionalità tra file in Node.js.
Spiegazione del sistema dei moduli di Node.js è una lezione Node.js Backend Development Bootcamp gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Node.js Backend Development Bootcamp, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Node.js Backend Development Bootcamp include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Organize Your Code with Modules
As apps grow, one giant file gets messy. Modules split code into small, reusable, focused pieces. Node supports two systems: CommonJS and ES Modules.
CommonJS: `require()` & `module.exports`
CommonJS is Node's original module system: require() pulls code in, module.exports pushes code out. Import what you need, export what you offer.
CommonJS: Exporting a Function
Here's mathUtils.js. Whatever you assign to module.exports is what other files can import from it — here, just the add function.
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
module.exports = add; // Only 'add' is exportedCommonJS: Importing and Using
In another file, require('./mathUtils.js') pulls in that exported function. Keep both files in the same folder and run node app.js.
const addFunction = require('./mathUtils.js');
console.log("Using the imported function:");
console.log(`5 + 3 = ${addFunction(5, 3)}`);
console.log(`10 + 2 = ${addFunction(10, 2)}`);CommonJS: Exporting Multiple Items
Need to export several things? Assign an object to module.exports holding all of them — a clean way to group related utilities.
const greet = (name) => `Hello, ${name}!`;
const farewell = (name) => `Goodbye, ${name}!`;
module.exports = {
greetUser: greet,
sayGoodbye: farewell
};CommonJS: Importing Multiple Items
When a module exports an object, grab pieces with destructuring: const { greetUser, sayGoodbye } = require(...). Cleaner and clearer than dot access.
const { greetUser, sayGoodbye } = require('./utils.js');
console.log(greetUser('Alice'));
console.log(sayGoodbye('Bob'));ES Modules: The Modern Standard
ES Modules (ESM) are JavaScript's official standard, using import/export. They enable tree-shaking — set "type": "module" or use .mjs to opt in.
ESM: Named Exports & Imports
With named exports, you export values by name and import them in matching { } braces — making it obvious exactly which parts you're using.
export const add = (a, b) => a + b;
export const multiply = (a, b) => a * b;
// app.mjs (main entry point)
// To run this: ensure package.json has "type": "module"
// and run with `node app.mjs`
import { add, multiply } from './calculator.mjs';
console.log(`2 + 4 = ${add(2, 4)}`);
console.log(`5 * 6 = ${multiply(5, 6)}`);ESM: Default Exports
A module can have one default export, usually its main feature. Import it under any name you like, no braces needed.
const logMessage = (msg) => console.log(`[LOG] ${msg}`);
export default logMessage;
// main.mjs (main entry point)
// To run this: ensure package.json has "type": "module"
// and run with `node main.mjs`
import myLogger from './logger.mjs';
myLogger('This is a default message.');
myLogger('Another important event.');Quick Check: Module Systems
You've learned about CommonJS and ES Modules. Let's test your understanding!
Recap: Node.js Modules
You've explored Node modules: CommonJS (require/module.exports, the classic) and ESM (import/export, the modern standard). Both keep apps organized.
Domande Frequenti
La lezione «Spiegazione del sistema dei moduli di Node.js» è gratuita?
Sì — il testo completo di «Spiegazione del sistema dei moduli di Node.js» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Node.js Backend Development Bootcamp, passa a CoddyKit PRO. Il corso Node.js Backend Development Bootcamp include 4 lezioni in totale.
Cosa imparerò in «Spiegazione del sistema dei moduli di Node.js»?
Esplori i moduli CommonJS, i moduli ES e il modo in cui importare ed esportare funzionalità tra file in Node.js. Eserciti Node.js Backend Development Bootcamp con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Node.js Backend Development Bootcamp?
Non è richiesta alcuna esperienza precedente. Node.js Backend Development Bootcamp su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Spiegazione del sistema dei moduli di Node.js»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Node.js Backend Development Bootcamp?
Sì. Ogni lezione Node.js Backend Development Bootcamp include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Introduzione a Node.js e NPM
- Spiegazione del sistema dei moduli di Node.js
- JavaScript asincrono ed event loop
- Utilizzo del file system e degli stream