Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Introduction to import Syntax | Node.js Architecture and the Module System
Node.js Foundations

bookIntroduction to import Syntax

When you work with Node.js, you will often need to organize your code into multiple files. To use code from one file in another, you need a way to import and export functionality. Node.js supports two main module systems: CommonJS (using require) and ES modules (using import). The ES module system, based on the JavaScript standard, is becoming increasingly popular and is recommended for modern Node.js projects.

The ES module syntax uses the import statement to bring in code from other files. You should use ES modules when you want compatibility with modern JavaScript tools, or when you are writing code that may run in both Node.js and browsers. ES modules are especially useful if you are using features like import/export, top-level await, or want to avoid some of the quirks of CommonJS.

index.js

index.js

copy
Note
Note

The CommonJS module system uses require() to load modules and module.exports or exports to export them. In contrast, ES modules use import and export statements. ES modules are statically analyzed, which enables better optimization and tooling, while CommonJS modules are loaded dynamically at runtime. Also, ES modules require the .mjs file extension or "type": "module" in your package.json to work in Node.js.

question mark

Which of the following statements best describes a key difference between require (CommonJS) and import (ES modules) in Node.js?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 9.09

bookIntroduction to import Syntax

Swipe um das Menü anzuzeigen

When you work with Node.js, you will often need to organize your code into multiple files. To use code from one file in another, you need a way to import and export functionality. Node.js supports two main module systems: CommonJS (using require) and ES modules (using import). The ES module system, based on the JavaScript standard, is becoming increasingly popular and is recommended for modern Node.js projects.

The ES module syntax uses the import statement to bring in code from other files. You should use ES modules when you want compatibility with modern JavaScript tools, or when you are writing code that may run in both Node.js and browsers. ES modules are especially useful if you are using features like import/export, top-level await, or want to avoid some of the quirks of CommonJS.

index.js

index.js

copy
Note
Note

The CommonJS module system uses require() to load modules and module.exports or exports to export them. In contrast, ES modules use import and export statements. ES modules are statically analyzed, which enables better optimization and tooling, while CommonJS modules are loaded dynamically at runtime. Also, ES modules require the .mjs file extension or "type": "module" in your package.json to work in Node.js.

question mark

Which of the following statements best describes a key difference between require (CommonJS) and import (ES modules) in Node.js?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3
some-alt