🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

NodeJS Modules [Simple Explanation]

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




What are modules?



Imagine you have an index.js file in your NodeJS project where you have used five functions. However, two of these functions might also be used in other files. So, instead of one file, you create three files in your project:




  • index.js

  • dependency1.js

  • dependency2.js



Here, each of these Javascript files is a module, and the way to export classes/functions and import them is basically the module system. A module system allows us to split and include code and import code written by us or other developers whenever required.



These modules are not just the Javascript files that exist in your project; they can also be any external package you have installed as a dependency in your project. Also, NodeJS has some built-in modules like http, fs, etc., which are available along with installation and can be imported without adding any external dependencies.






How can I export or import a module?



Two module systems are used in Node.




  1. CommonJS (CJS)

  2. ECMAScript Module (ESM)



You have read the kitchen-chef-waiter example in my previous blog; Similarly, if we compare modules with that, imagine CommonJS is an old big recipe book we needed to search and find a recipe, while ESM is a new aged digital app to see recipes.



CommonJS (CJS)




  • The older module system.

  • Modules are written in a simple format like:




CODE
// Export
module.exports = function SayHello() {
console.log("Hello World!");
};

// Import
const GetHello = require("./hello-script.js");
SayHello(); // "Hello World!"







  • Synchronous: Loads modules one after another.

  • Works only in Node.js (not directly in browsers).



ECMAScript Modules (ESM)




  • The modern module system.

  • Modules are now more structured:




CODE
// Export
export function SayHello() {
console.log("Hello World!");
}

// Import
import { SayHello } from "./hello-script.js";
SayHello();







  • Asynchronous: Loads multiple modules at the same time.

  • Works natively in browsers and Node.js.



Key Difference in syntax:

CJS: module.exports / require

ESM: export / import





How do I configure CJS or ESM in my project?




  • Open the package.json of your project.

  • Add:



CODE
{
type: "module";
}






  • This tells NodeJS to interpret .js as ESM. If we don't add this, NodeJS will interpret it as CommonJS by default.





Modules in Typescript:



Sometimes, you can use ECMAScript modules but an old package you have imported is written in CommonJS. To handle these cases, we sometimes make sure the output Javascript code generated from the Typescript file comes in the common format, even though we have written the Typescript files in ESM format.



For this, we add the compilerOptions in the tsconfig.json of our project:




CODE
"compilerOptions": {
"module": "commonjs",
"target": "es6"
}






What happens then:

module: "commonjs": Outputs JavaScript using the CommonJS module system, which uses require and module.exports.

target: "es6": Ensures that the output JavaScript uses ES6 syntax and features like let, const, and arrow functions.



Input Typescript code:




CODE
import { readFile } from "fs";

const SayHello = () => {
console.log("Hello World!");
};

export { SayHello };






Output Javascript code:




CODE
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SayHello = void 0;
const fs_1 = require("fs");
const SayHello = () => {
console.log("Hello World!");
};
exports.SayHello = SayHello;


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten NodeJS Modules [Simple Explanation]

Thematisch verwandte Begriffe: NodeJS, Modules, Simple, Explanation · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...