Das Node.js-Modulsystem erklärt
Erkunden Sie CommonJS-Module und ES-Module und erfahren Sie, wie Sie in Node.js Funktionalität dateiübergreifend importieren und exportieren.
Das Node.js-Modulsystem erklärt ist eine kostenlose Node.js Backend Development Bootcamp-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Node.js Backend Development Bootcamp-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Node.js Backend Development Bootcamp-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Das Node.js-Modulsystem erklärt“ kostenlos?
Ja — der vollständige Text von „Das Node.js-Modulsystem erklärt“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Node.js Backend Development Bootcamp-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Node.js Backend Development Bootcamp-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Das Node.js-Modulsystem erklärt“?
Erkunden Sie CommonJS-Module und ES-Module und erfahren Sie, wie Sie in Node.js Funktionalität dateiübergreifend importieren und exportieren. Du übst Node.js Backend Development Bootcamp mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Node.js Backend Development Bootcamp zu starten?
Keine Vorkenntnisse erforderlich. Node.js Backend Development Bootcamp auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Das Node.js-Modulsystem erklärt“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Node.js Backend Development Bootcamp-Lektion Code schreiben und ausführen?
Ja. Jede Node.js Backend Development Bootcamp-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einführung in Node.js und NPM
- Das Node.js-Modulsystem erklärt
- Asynchrones JavaScript und Event Loop
- Arbeiten mit Dateisystem und Streams